hasattr + __getattr__: I think this is Python bug

Ethan Furman ethan at stoneleaf.us
Mon Jul 26 12:47:19 EDT 2010


Bruno Desthuilliers wrote:
> 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.

Well, it is Monday, so I may be missing something obvious, but what is 
the effective difference between these two solutions?

~Ethan~



More information about the Python-list mailing list