AttributeError: ClassA instance has no attribute '__len__'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MackS

    #1

    AttributeError: ClassA instance has no attribute '__len__'

    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
    [color=blue][color=green][color=darkred]
    >>> a = foo()
    >>> a.fun(2)
    >>>[/color][/color][/color]

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

    Thanks in advance

    Mack

  • Michael Spencer

    #2
    Re: AttributeError: ClassA instance has no attribute '__len__'

    MackS wrote:[color=blue]
    > 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:
    >[/color]
    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...
    [color=blue]
    > AttributeError: ClassA instance has no attribute '__len__'
    >
    > Following the traceback,I see that the offending line is
    >
    > self.x = arg1 + len(self.y) + 1[/color]

    len calls the object's __len__ method. self.y is bound to something (an
    instance of ClassA) that apparently has no __len__ method
    [color=blue]
    >
    > 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
    >
    >[color=green][color=darkred]
    >>>>a = foo()
    >>>>a.fun(2)
    >>>>[/color][/color]
    >[/color]
    In this case self.y is bound to something different i.e., 'y' :an object of type
    str, which has a __len__ method:[color=blue][color=green][color=darkred]
    >>> 'y'.__len__()[/color][/color][/color]
    1

    Michael



    Comment

    • vincent wehren

      #3
      Re: AttributeError: ClassA instance has no attribute '__len__'

      "MackS" <mackstevenson@ hotmail.com> schrieb im Newsbeitrag
      news:1112209152 .554037.194030@ f14g2000cwb.goo glegroups.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
      |


      Comment

      • wittempj@hotmail.com

        #4
        Re: AttributeError: ClassA instance has no attribute '__len__'

        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

        Comment

        Working...