2015-02-09 27 views
-3

我有以下代碼:如何調用父初始化

class TitleManager(): 
    def __init__(self): 
     # do stuff 

class Scrape(TitleManager): 
    def __init__(self): 
     self.url = '' 
     self.init_parent() # how to do this? 

我怎麼會叫的TitleManagerScrape init方法初始化?

回答

1

在Python2或Python3中。

class Scrape(TitleManager): 
    def __init__(self): 
     super(Scrape, self).__init__() 
     ... 

在Python3:

class Scrape(TitleManager): 
    def __init__(self): 
     super().__init__() 
     ... 

這裏是super

+0

相關的文檔,如果我沒記錯的話.... – 2015-02-10 00:11:05

+1

你做在Python 2.x的用戶不能'super' @ PythonMaster 2.2版本的新功能。你不能對舊式類使用'super',但那些從2.2版開始已被棄用,並且在Python3中被完全刪除。基本上,如果你在過去的12年中更新了Python,你不應該有這個問題:) – 2015-02-10 00:14:54