Skip to content Skip to sidebar Skip to footer

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)

Solution 1:

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

Post a Comment for "Python No Output From Class Method"