hasattr + __getattr__: I think this is Python bug
Bruno Desthuilliers
bruno.42.desthuilliers at websiteburo.invalid
Mon Jul 26 09:07:49 EDT 2010
Duncan Booth a écrit :
> Bruno Desthuilliers <bruno.42.desthuilliers at websiteburo.invalid> wrote:
>
>> If you don't want to create as many Whatever instances as MyClass
>> instances, you can create a single Whatever instance before defining
>> your class:
>>
>> DEFAULT_WHATEVER = Whathever()
>>
>> class MyClass(object):
>> def __init__(self, x, y):
>> self.x = x
>> self.y = y
>> self.size = DEFAULT_WHATEVER
>>
>>
>
> Or you could create the default as a class attribute
from the OP:
"""
I have a class (FuncDesigner oofun) that has no attribute "size", but
it is overloaded in __getattr__, so if someone invokes
"myObject.size", it is generated (as another oofun) and connected to
myObject as attribute.
"""
so this solution won't obviously work in this case !-)
Also and FWIW, I wouldn't advocate this solution if the "default" class
attribute is of a mutable type.
More information about the Python-list
mailing list