Python's equivalent of static member data?

Erik Max Francis max at alcyone.com
Thu May 8 06:21:33 EDT 2003


Bram Stolk wrote:

> In C++, you can have static member data: only one instance of the
> data,
> shared by all instances of the class.
> 
> What is the equivalent in Python?

Class-scope attributes:

>>> class C:
...  X = 10
...  def __init__(self, x):
...   self.x = x
... 
>>> c = C(1)
>>> c.x
1
>>> c.X
10
>>> C.X
10
>>> C.X = 20
>>> c.X
20
>>> c.x
1

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ Then you give me that Judas Kiss / Could you hurt me more than this
\__/ Lamya
    Polly Wanna Cracka? / http://www.pollywannacracka.com/
 The Internet resource for interracial relationships.




More information about the Python-list mailing list