0

I have the following code:

class Stat(list):
    def __init__(self, lst = []):
        self.s = list(lst)

    def __repr__(self):
        return "Stat({})".format(self.s)

    def add(self, item):
        self.s.append(item)

    def len(self):       
        return len(self.s)

...(more methods, but not necessary)

All of the methods work properly but len(). No matter the length of the Stat object, the returned length is always 0; I don't understand why.

10
  • 1
    How are you creating the Stat object and calling the len method? Commented Apr 19, 2016 at 5:43
  • first add some item in 1st array Commented Apr 19, 2016 at 5:46
  • 1
    You probably shouldn’t be inheriting from list. Commented Apr 19, 2016 at 5:46
  • 5
    If you want to be able to do len(Stat()), the special method name is __len__. The right way to call the len method (as it’s just a method named len) on your current class is just Stat().len(). But you wouldn’t need to inherit from list to do either of these. Pick one of wrapping list (having self.s) or inheriting from it. Commented Apr 19, 2016 at 5:48
  • 2
    On the note of not inheriting from list, further reading here Commented Apr 19, 2016 at 5:53

4 Answers 4

7

it will return 0 always when you are using it like this:

x = Stat([1,3,4,5])
print len(x)

if you want to override len function use this code:

def __len__(self):       
    return len(self.s)
Sign up to request clarification or add additional context in comments.

2 Comments

This solved it! Don't know why I didn't try this... Thanks!
@Frank I think you inherited from list in order to be able to do len(myobj). Seriously consider not inheriting from list now that you know that def __len__(self) is the key to be able to do len(myobj).
0
s = Stat([1, 2])
s.add(1)
s.add(2)
print s.len()

I have run your code, the result is correct in my environment.

2 Comments

When calling the length method it should be called as len(object)
@Frank You have to override __len__ then
0

Override the magic method __len__(self) to control the output of a call to len(my_stat_object):

class Stat(list):
    def __init__(self, lst = []):
        self.s = list(lst)

    def __repr__(self):
        return "Stat({})".format(self.s)

    def add(self, item):
        self.s.append(item)

    def __len__(self):       
        return len(self.s)

Comments

0

If what you're trying to run is len(stat) and not stat.len(), your function len should be named __len__ instead. Here's the docs: object.len

stat = Stat([1, 2])
len(s) # 0 if len, 2 if __len__

As a side note, you might want to replace lst=[] in your init definition, as it can cause some weird looking behaviours. Read about it here: mutable default argument

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.