By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,402 Members | 1,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,402 IT Pros & Developers. It's quick & easy.

Weird UserArray AttributeError (bug ?)

P: n/a
I came across a strange error when trying to define a settable property
for a new-style subclass of UserArray (Numeric). Here's a shorter
example that reproduces the problem:
from UserArray import UserArray
from math import hypot

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

def _fget(self): return hypot(*self)
def _fset(self, m): self *= m/self.magnitude
magnitude = property(_fget, _fset)
v = Vector(3.,4.)
print v.magnitude
# the line below prints an infinite sequence of:
# Exception exceptions.AttributeError: "can't delete attribute"
# in ignored
v.magnitude = 10
Any ideas on what's going on and if there's a workaround ?

George

Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.