hasattr + __getattr__: I think this is Python bug

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Mon Jul 26 06:20:59 EDT 2010


dmitrey a écrit :
(snip)

> This doesn't stack with the following issue: sometimes user can write
> in code "myObject.size = (some integer value)" and then it will be
> involved in future calculations as ordinary fixed value; if user
> doesn't supply it, but myObject.size is involved in calculations, then
> the oofun is created to behave like similar numpy.array attribute.

IOW, you want a default value for the size if it has not been specified 
by the user, so you can safely use this attribute in computations. The 
more straightforward solution is to define this attribute (with the 
default value) in the initialiser, ie:


class MyClass(object):
     def __init__(self, x, y):
         self.x = x
         self.y = y
         self.size = Whatever()


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


HTH



More information about the Python-list mailing list