[Tutor] class (or static) variable?

antonmuhin at rambler.ru antonmuhin at rambler.ru" <antonmuhin@rambler.ru
Sun Apr 13 12:24:02 2003


Hello Thomas,

Sunday, April 13, 2003, 7:39:46 AM, you wrote:

TR> Hello--

TR> Is MyClass.item in the following an example of a class (Java/C++ 'static')
TR> variable?

TR> #!/bin/env python
TR> class MyClass:
TR>         item=42
TR>         def __init__(self):
TR>                 self.item = 24
TR>         def __str__(self):
TR>                 return str(self.item)
TR> x=MyClass()
TR> print x.item
TR> print x
TR> print MyClass.item
TR> # prints:
TR> # 24
TR> # 24
TR> # 42

TR> Thanks,

TR> Tom
TR> --
TR> I like Boolean logic. NOT

I suppose, yes:

PythonWin 2.2.1 (#34, Apr 15 2002, 09:51:39) [MSC 32 bit (Intel)] on win32.
Portions Copyright 1994-2001 Mark Hammond (mhammond@skippinet.com.au) - see 'Help/About PythonWin' for further copyright information.
>>> class Foo:
...     a = 42
...     def __init__(self):
...             self.a = 24
...             
>>> foo = Foo()
>>> foo.a
24
>>> Foo.a
42
>>> id(foo.a)
9816512
>>> id(Foo.a)
9816332
>>>

hth,

-- 
Best regards,
 anton                            mailto:antonmuhin@rambler.ru