gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:12:58 25 4
gpt4 key购买 nike

任何人都可以用正确的语法帮助我从父类调用我的方法 __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(...)

关于python - 在 Python 中从父类调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17709040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com