user3517846
user3517846

Reputation: 1

Python no output from class method

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()

Upvotes: 0

Views: 824

Answers (1)

jonrsharpe
jonrsharpe

Reputation: 121955

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()

Upvotes: 3

Related Questions