Skip to main content
 首页 » 编程设计

python之如何通过继承扩展 init 方法

2025年05月04日14think

我有以下代码,我在其中尝试扩展 BaseExporter 的 init 方法:

from apps.ingest.platform_export import BaseExporter 
 
class Vudu(BaseExporter): 
 
    def __init__(self): 
        BaseExporter.__init__() 
        self.platform = ' Vudu' 

基本上,我想要来自 BaseExporter 的所有初始化变量加上 self.platform = 'Vudu' 的附加变量。我该如何正确地做到这一点?

请您参考如下方法:

python 3

from apps.ingest.platform_export import BaseExporter 
 
class Vudu(BaseExporter): 
    def __init__(self): 
        super().__init__() 
        self.platform = ' Vudu' 

python 2

from apps.ingest.platform_export import BaseExporter 
 
class Vudu(BaseExporter): 
    def __init__(self): 
        super(Vudu, self).__init__() 
        self.platform = ' Vudu'