B
Bruno Desthuilliers
Marc 'BlackJack' Rintsch a écrit :
In the __init__() ?
Yes indeed, it works. But it means that:
1/ the attributes are not set on the class before it has been
instanciated at least once
2/ each time you instanciate the class, the attributes are rebound to
new MyDesc instances
class MyDesc(object):
def __init__(self, name=None, initVal=None):
self.name = name
self.value = initVal
print "new %s" % self.__class__.__name__
def __get__(self, obj, objtype):
# Do some stuff
#self.value = "blah"
if obj is None:
return self
return self.value
class Marc(object):
attributes = ('toto', 'tata')
def __init__(self):
cls = self.__class__
for attr in cls.attributes:
setattr(cls, attr, MyDesc(attr, attr))
Traceback (most recent call last):
new MyDesc
new MyDescnew MyDesc
new MyDesc
So while it "works" (kinda), I would not recommand this solution. Even
if you do test for the existence of the attribute before rebinding it,
you'll have to go thru the whole dance each time you instanciate the class.
My 2 cents...
Ok, that "exec" is an ugly hack. There's gotta be someway to plop
this straight into the class's __dict__ without doing that, but when I
try adding self.__class__.__dict__[attr] = MyDesc(attr) in MyClass's
__init__ method, I get the error: "TypeError: 'dictproxy' object does
not support item assignment"
Does ``setattr(self.__class__, attr, MyDesc(attr))`` work?
In the __init__() ?
Yes indeed, it works. But it means that:
1/ the attributes are not set on the class before it has been
instanciated at least once
2/ each time you instanciate the class, the attributes are rebound to
new MyDesc instances
class MyDesc(object):
def __init__(self, name=None, initVal=None):
self.name = name
self.value = initVal
print "new %s" % self.__class__.__name__
def __get__(self, obj, objtype):
# Do some stuff
#self.value = "blah"
if obj is None:
return self
return self.value
class Marc(object):
attributes = ('toto', 'tata')
def __init__(self):
cls = self.__class__
for attr in cls.attributes:
setattr(cls, attr, MyDesc(attr, attr))
Traceback (most recent call last):
new MyDesc
new MyDescnew MyDesc
new MyDesc
So while it "works" (kinda), I would not recommand this solution. Even
if you do test for the existence of the attribute before rebinding it,
you'll have to go thru the whole dance each time you instanciate the class.
My 2 cents...