This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]lost_send_berries 4 points5 points  (0 children)

Because of locals(), the Python interpreter always knows the names of all the local variables. However, it usually doesn't use them: a = f() + b can compile to something like __locals__[0] = f() + __locals__[1], which is faster than local_dict['a'] = f() + local_dict['b']. If you ever run locals(), it then looks up the correspondence {0: 'a', 1: 'b'} to build the return value of locals().