AttributeError: ClassA instance has no attribute '__len__'

wittempj@hotmail.com martin.witte at gmail.com
Wed Mar 30 22:01:06 CEST 2005


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




More information about the Python-list mailing list