hasattr + __getattr__: I think this is Python bug

Duncan Booth duncan.booth at invalid.invalid
Mon Jul 26 06:55:28 EDT 2010


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 and it can be 
overridden in those instances which need a different value.

class MyClass(object):
    size = Whatever()

    def __init__(self, x, y):
        self.x = x
        self.y = y

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list