The following prints {'a': 'a', 'b': 'b'}
:
def foo(a: str = "a", b: str = "b") -> None:
print(locals())
foo() # Prints {'a': 'a', 'b': 'b'}
Which I'd expect as locals
in Python 3.7+ returns the order of creation.
But the below prints {'b': 'b', 'a': 'a'}
def foo(a: str = "a", b: str = "b") -> None:
print(locals())
lambda: a
# or:
# def inner() -> None:
# a
foo() # Prints {'b': 'b', 'a': 'a'}
It seems like it delayed the order of variable creation, which is strange. Why is this?
Edit: Ah, this only happens with Python 3.10 and lower. I guess it's a Python issue? If anyone could find the relevant GitHub issue, please share it. I might be blind as I can't find the relevant details in the 3.11 changelogs.
lambda
,a
would have continued to exist indefinitely). It's not surprising to me that this affects its position in thelocals()
dict. – Chemisorptionlocals()
is a function that gathers up all the local variables, because they're not necessarily all in one place. – Cos