This works as expected: a class variable is created with a value of 12 .
>>> a = 12 >>> class K(object): ... a = a ... >>> print Ka 12
But when I try to do the same inside the function:
>>> def f(a): >>> class K(object): ... a = a ... >>> f(12) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in f File "<stdin>", line 3, in K NameError: name 'a' is not defined
However, this works without errors:
>>> def f(a): ... a = a ... >>> f(12)
What's happening? (This is Python 2.7).
source share