0

When I call the displaycount method of my list subclass, I get no output. What is the problem?

class mylist(list):

    def _init_(self,name,age):
        list._init_(self, years)
        self.name = name;
        self.age = age;

    def displaycount(self):
        print "total employees %d" % self.name
        emp1 = mylist('lada', 20)
        emp1.displaycount()
8
  • 2
    did you call displaycount? Commented Apr 12, 2014 at 8:58
  • You have to instantiate the mylist class for it's __init__ method to be called. You'll also have to call the displaycount and perhaps fix your indentation Commented Apr 12, 2014 at 8:59
  • It looks like you are calling displaycount only inside itself, which might cause infinite recursion if you actually call it once from outside ... Commented Apr 12, 2014 at 8:59
  • Please be more specific. What do you mean by "blank result"? Could you should the calling code, and review your indentation - it looks like displaycount is nested inside _init_ (which has the wrong number of underscores!) Commented Apr 12, 2014 at 9:00
  • 2
    @icedtrees - just so you know, your edit (while correct) might just have solved the OP's problem. In the future, please refrain from making edits to any code blocks without first confirming with the OP. Commented Apr 12, 2014 at 9:03

1 Answer 1

3

You have a few problems:

  1. '_init_' != '__init__';
  2. Where is years defined?
  3. Given that self.name is a string, what do you think "total employees %d" % self.name will do?
  4. displaycount currently recursively calls itself on a new mylist instance.

Perhaps you mean:

class mylist(list):

    def __init__(self, name, age):
        super(mylist, self).__init__()
        self.name = name
        self.age = age

    def displaycount(self):
        print "total employees {0}".format(self.age)


emp1 = mylist('lada', 20)
emp1.displaycount()
Sign up to request clarification or add additional context in comments.

7 Comments

@jonrshape so while calling we must use init instead of init right ? and i need to define this.years also right ?
@user3517846 I don't have enough information about what you're doing - I have added a suggested implementation
why you called super(mylist, self).__init__() isntead of list._init_(self, years). i think list._init_(self, years) is also correct..
list.__init__(self) - two underscores, and you still haven't defined years
yeah i got and i have one doubt too ..is super(mylist, self).__init__() and list.__init__(self) do the same function ?
|

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.