How do I declare global vars or class vars in Python ?
Bruno Desthuilliers
bruno.42.desthuilliers at websiteburo.invalid
Fri Feb 20 08:04:15 EST 2009
Nick Craig-Wood a écrit :
(snip)
> Note that in python we don't normally bother with getA/setA normally,
> just use self.A, eg
>
> class Stuff(object):
> def __init__(self):
> self.A = None
> self.B = None
> def main(self):
> print self.A
> print self.B
> # dosomething
> self.A = "aValue"
> self.B = "aValue"
> print self.A
> print self.B
>
>>>> a = Stuff()
>>>> a.main()
> None
> None
> aValue
> aValue
>>>>
>
> If you need (later) A to be a computed value then you turn it into a
> property, which would look like this. (Note the main method is
> identical to that above).
>
> class Stuff(object):
> def __init__(self):
> self._A = None
Note that while you *can* do direct access to the implementation
attribute (here, '_A' for property 'A'), you don't *need* to so (and
usually shouldn't - unless you have a very compelling reason).
More information about the Python-list
mailing list