Closure/method definition question for Python 2.7
Marko Rauhamaa
marko at pacujo.net
Mon Mar 10 13:36:24 EDT 2014
"Brunick, Gerard:(Constellation)" <Gerard.Brunick at constellation.com>:
> class Test(object):
> x = 10
>
> def __init__(self):
> self.y = x
>
> t = Test()
> ---
>
> raises
>
> NameError: global name 'x' is not defined.
In the snippet, x is neither local to __init__() nor global to the
module. It is in the class scope. You can refer to it in one of two
ways:
Test.x
or:
self.x
Marko
More information about the Python-list
mailing list