[Tutor] Odd AttributeError
Magnus Lyckå
magnus@thinkware.se
Fri May 23 01:04:02 2003
At 06:56 2003-05-21 -0700, Neil Hodge wrote:
>So, why does self.as_tuple not exist right after I defined it? Thanks.
Because you didn't really define it? Is a __setattr__ method defined
for the class? Does it eat your definition?
See below:
>>> class X:
... def __init__(self, x):
... self.x = x
... print "self.x is", self.x
... def __setattr__(self, attr, val):
... pass
...
>>> x = X(42)
self.x is
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 4, in __init__
AttributeError: X instance has no attribute 'x'
You can try doing "self.__dict__['as_tuple'] = as_tuple" instead.
Then you avoid calling __setattr__ since you don't rebind any
attribute, just modify a current one.
>>> class X:
... def __init__(self, x):
... self.__dict__['x'] = x
... print "self.x is", self.x
... def __setattr__(self, attr, val):
... pass
...
>>> x = X(42)
self.x is 42
--
Magnus Lycka (It's really Lyckå), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The shortest path from thought to working program