class variables
Bruno Desthuilliers
onurb at xiludom.gro
Mon Jul 31 06:09:17 EDT 2006
Colin J. Williams wrote:
> Andre Meyer wrote:
>> Hi all
>>
>> I am trying to understand the magic of Python's class variables and
>> tried the following code (see below).
>>
>> Just out of curiosity, I tried to define a property that provides
>> access to a seemingly instancae variable which is in fact a class
>> variable.
class Foo(object):
_v = 5
@apply
def v():
def fget(self):
return Foo._v
# other solution [1] :
# return self.__class__._v
def fset(self, val):
Foo._v = val
# other solution [1] :
# self.__class__._v = val
return property(**locals())
[1] Depends on how you want this to work when subclassing Foo...
Gives :
>>> f1 = Foo()
>>> f2 = Foo()
>>> f1.v
5
>>> f2.v
5
>>> f1.v = 42
>>> f1.v
42
>>> f2.v
42
>>> Foo._v
42
>>>
>> All seems to work fine (case 4), but when a custom object is
>> assigned, an instance variable is created instead of using theproerty
>> (case 5).
>>
>> What goes wrong here?
I'm afraid there are too much problems with indentation in the posted
code to give any serious answer.
(snip)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"
More information about the Python-list
mailing list