Skip to main content
 首页 » 编程设计

python之在 Python 中从父类调用方法

2025年05月04日10qq78292959

任何人都可以用正确的语法帮助我从父类调用我的方法 __get_except_lines(...) 吗?

我有一个类,其方法如下所示。这个特殊的方法有 2 个下划线,因为我不希望“用户”使用它。

NewPdb(object) 
    myvar = ... 
    ... 
    def __init__(self): 
        ... 
    def __get_except_lines(self,...): 
        ... 

在一个单独的文件中,我有另一个继承自此类的类。

from new_pdb import NewPdb 
 
    PdbLig(NewPdb): 
        def __init__(self): 
            .... 
            self.cont = NewPdb.myvar 
            self.cont2 = NewPdb.__get_except_lines(...) 

我得到了一个让我很困惑的属性错误:

AttributeError: type object 'NewPdb' has no attribute '_PdbLig__get_except_lines' 

请您参考如下方法:

您的问题是由于私有(private)变量 (http://docs.python.org/2/tutorial/classes.html#private-variables-and-class-local-references) 的 Python 名称重整造成的。你应该写:

NewPdb._NewPdb__get_except_lines(...)