lundi 10 mars 2014

Closure/method definition question for Python 2.7 topic




"Brunick, Gerard:(Constellation)" <(E-Mail Removed)>:

> 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





Aucun commentaire:

Enregistrer un commentaire