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

AttributeError: ClassA instance has no attribute '__len__'

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

AttributeError: ClassA instance has no attribute '__len__'

Following the traceback,I see that the offending line is

self.x = arg1 + len(self.y) + 1

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)


No problems; can you help me make some sense of what is happening?

Thanks in advance

Mack

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


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

Jul 18 '05 #2

P: n/a
"MackS" <ma***********@hotmail.com> schrieb im Newsbeitrag
news:11**********************@f14g2000cwb.googlegr oups.com...
| 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:
|
| AttributeError: ClassA instance has no attribute '__len__'
|
| Following the traceback,I see that the offending line is
|
| self.x = arg1 + len(self.y) + 1
|
| 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)
| >>>
|
| No problems; can you help me make some sense of what is happening?

In your program, self.y is an instance of ClassA. The traceback tells you
that ClassA has no __len__ attribute (i.e.
it is an object that has no no "special" method called __len__, which is
what gets called
when you do len(obj).

In your test program, self.y is "y", a string, which has a __len__ method by
design:
(see dir("y"), which gives you:

['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__',
'__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
'__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__',
'__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__',
'__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith',
'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower',
'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip',
'replace', 'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split',
'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate',
'upper', 'zfill']

If you want len(self.y) to work, self.y must be an object that implements a
__len__ method. In other words, your "ClassA" needs a __len__ method.

A trivial example:

class ClassA:
def __init__(self, text):
self.text = text
def __len__(self):
#return something useful
return len(self.text)

y = ClassA("Hello")
print len(y) # prints 5
Regards,

--
Vincent Wehren
|
| Thanks in advance
|
| Mack
|
Jul 18 '05 #3

P: n/a
The most simple way to get this error I can think of is like this. It
happens because len does not know how to calculate the lenght of this
object.
-class classA:
- def __init__(self):
- pass

-a = classA()
-print len (a)

Traceback (most recent call last):
File "./test.py", line 10, in ?
print len (a)
AttributeError: classA instance has no attribute '__len__'

You can fix it by adding a __len__ method:
class classA:
def __init__(self):
pass

def __len__(self):
return 0

a = classA()
print len (a)

See http://docs.python.org/ref/sequence-types.html

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.