class employee(object):
def __init__(self,employeenumber,name):
self.employeenumber=employeenumber
self.name=name
def printdata(self):
print self.employeenumber
print self.name
class payroll(employee):
def __init__(self,employeenumber,name,salary):
employee. __init__(employeenumber,name)
self.salary=salary
def outdata(self):
self.printdata()
print self.salary
class leave(payroll):
def __init__(self,employeenumber,name,salary,nodays):
payroll. __init__(employeenumber,name,salary)
self.nodays=nodays
def showdata(self):
self.outdata()
print self.nodays
emp1=leave(3,"sam",5000,8)
emp1.showdata()
Tried using "Super" but was getting the same error:
Traceback (most recent call last):
File "C:/Python27/limb.py", line 23, in <module>
emp1=leave(3,"sam",5000,8)
File "C:/Python27/limb.py", line 17, in __init__
payroll. __init__(employeenumber,name,salary)
TypeError: unbound method __init__() must be called with payroll instance as first argument (got int instance instead)
It would be great if someone could suggest writing this code with super so that i can understand how it actually functions.
To call the overridden __init__
method directly on the class, you need to explicitly pass in self
:
class leave(payroll):
def __init__(self, employeenumber, name, salary, nodays):
payroll.__init__(self, employeenumber, name, salary)
self.nodays=nodays
because methods looked up on a class are not bound to an instance (since Python cannot know what the instance would be in that case). As such you passed employeenumber
as the first argument instead, which is not a valid type of object to use as the self
argument for that method.
Alternatively, use the super()
function , which produces a bound method (so a method object where self
has been bound to an instance):
class leave(payroll):
def __init__(self, employeenumber, name, salary, nodays):
super(leave, self).__init__(employeenumber, name, salary)
self.nodays=nodays
You should use class like this:
class employee(Thread):
instead of this:
class employee(object):
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.