Python Forum

Full Version: What is this syntax?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

Can anyone please explain me whats going on here? Ive never seen this syntax before.

    def judgeCircle(cls, moves):

       def moves_xy(moves):
            return {
                'R': [1, 0],
                'L': [-1, 0],
                'U': [0, 1],
                'D': [0, -1]
            }[moves]

        x = 0
        y = 0

        for move_temp in moves:
            coor = moves_xy(move_temp)
            x = x + coor[0]
            y = y + c[icode]oor[1]
Im really puzzled with the syntax moves_xy. Can anyone explain me understand this new format?
It's a dictionary defined and then a value is immediately pulled from it. The dictionary could be moved out into a global constant, and then "moves" fetched from it and it would be semantically the same.
Ofcourse. Apparently this approach is much faster compared to others. In fact very fast. Not sure how.
It uses a hash, so that helps. The if/elif/elif/elif alternative has to check the first three values anytime it turns out it needs the fourth.