2017-04-11 96 views
0

以下是我在Python繼承中的示例代碼。super().__ init __()失敗,錯誤

class db_Conn: 
    hike = 1.04 

    def __init__(self,first,last,pay): 
      self.first = first 
      self.last = last 
      self.pay = pay 
      self.email = first + '.' + last + '@ibm.com' 


    def full_name(self): 
      return'{} {}'. format(self.first, self.last) 

    def emp_raise(self): 
      self.pay = int(self.pay * self.hike) 


emp1 = db_Conn('amitesh','sahay',50000) 
emp2 = db_Conn('amit','sharma',60000) 

class Dev(db_Conn): 
    def __init__(self,first,last,pay,prog): 
     super().__init__(first,last,pay) 
     self.prog = prog 

dev1 = Dev('amitesh','sahay',50000, 'python') 
dev2 = Dev('amit','sharma',60000,'scala') 

print (dev1.prog) 
print(dev2.email) 

我得到下面的錯誤::

Traceback (most recent call last): 
dev1 = Dev('amitesh','sahay',50000, 'python') 
    super().__init__(first,last,pay) 
TypeError: super() takes at least 1 argument (0 given) 

我無法找出什麼錯誤我在做。請幫忙....!!!

回答

1

documentation for super()顯示它至少需要一個參數:要開始搜索的類。這是在Python 3製造可選的,但是當你正在使用2.7,你將需要:

 super(Dev).__init__(first,last,pay) 

它還說:

Note:super() only works for new-style class es.

新式的類從object繼承,這你沒有按「T。您需要申報db_Conn類:

class db_Conn(object):