AttributeError: ClassA instance has no attribute '__len__'
Michael Spencer
mahs at telcopartners.com
Wed Mar 30 14:14:29 EST 2005
MackS wrote:
> I'm new to Python. In general I manage to understand what is happening
> when things go wrong. However, the small program I am writing now fails
> with the following message:
>
In general you are more likely to get helpful responses from this group if you
post the actual code that has the problem and include the actual traceback.
However...
> AttributeError: ClassA instance has no attribute '__len__'
>
> Following the traceback,I see that the offending line is
>
> self.x = arg1 + len(self.y) + 1
len calls the object's __len__ method. self.y is bound to something (an
instance of ClassA) that apparently has no __len__ method
>
> Why should this call to the built-in len() fail? In a small test
> program it works with no problems:
>
> class foo:
> def __init__(self):
> self.x = 0
> self.y = 'y'
>
> def fun(self, arg1):
> self.x = arg1 + len(self.y) + 1
>
>
>>>>a = foo()
>>>>a.fun(2)
>>>>
>
In this case self.y is bound to something different i.e., 'y' :an object of type
str, which has a __len__ method:
>>> 'y'.__len__()
1
Michael
More information about the Python-list
mailing list