Weird UserArray AttributeError (bug ?)

George Sakkis gsakkis at rutgers.edu
Fri May 6 13:21:36 EDT 2005


To answer my own question, the error is caused by the __setattr__
defined in UserArray:
def __setattr__(self,attr,value):
        if attr=='shape':
            self.array.shape=value
        self.__dict__[attr]=value


I'm not sure though how to "undefine" __setattr__ in a subclass so that
property lookup works correctly. The furthest I got was to remove
__setattr__ from UserArray, but this is obviously unsafe if UserArray
is going to be used alone:

delattr(UserArray,'__setattr__')

class Vector(UserArray,object):
    def __init__(self,x,y):
        UserArray.__init__(self, (x,y))

    shape = property(lambda self: self.array.shape,
                     lambda self,v: setattr(self.array,'shape',v))

    magnitude = property(lambda self: hypot(*self),
        # setting scales the vector
        lambda self,magnitude: self.__imul__(magnitude/self.magnitude))


Any alternatives to get the same effect without deleting an attribute
of the superclass ?

George




More information about the Python-list mailing list