'super' 对象没有属性 '_Parent__method'
'super' object has no attribute '_Parent__method'
我已经阅读了所有关于此主题的类似帖子,但 none 我发现它们与我的问题相关,帮助我弄清楚发生了什么。
class A:
def __init__(self, value):
self.__value = value
self.__conn = httpsconnection # Specifics don't matter
class B(A):
def __init__(self, id, type, value):
super().__init__(value)
self.data = self.__create_sub_class(id, type)
def __create_sub_class(self, id, type):
self.__conn.request(...)
...
return data
class C(B):
def __init__(self, id, value):
super().__init__(id, externalVariable, value)
我收到的错误是 AttributeError: 'C' object has no attribute '_B__conn'
class C
不应该从 B
继承变量,而 B
是从 A
继承变量吗?
如果您想在子classes 中使用这些名称,请不要使用前导双下划线名称。 __name
明确设计为使名称 class 私有 ,即仅对确切的 class 有用。这个想法是,您可以在框架中使用此类名称,而无需限制 subclasses 可以使用的名称。
这样的名字在编译时被破坏;他们有 _ClassName
前缀(使用当前的 class 名称)。如果您想表明名称是内部名称,只需使用单下划线名称即可(Python 没有实际的隐私模型,名称始终可访问):
class A:
def __init__(self, value):
self._value = value
self._conn = httpsconnection # Specifics don't matter
class B(A):
def __init__(self, id, type, value):
super().__init__(value)
self.data = self._create_sub_class(id, type)
def _create_sub_class(self, id, type):
self._conn.request(...)
...
return data
参见词法分析文档中的Reserved classes of identifiers:
__*
Class-private names. Names in this category, when used within the context of a class definition, are re-written to use a mangled form to help avoid name clashes between “private” attributes of base and derived classes.
和引用的 documentation on names:
Private name mangling: When an identifier that textually occurs in a class definition begins with two or more underscore characters and does not end in two or more underscores, it is considered a private name of that class. Private names are transformed to a longer form before code is generated for them. The transformation inserts the class name, with leading underscores removed and a single underscore inserted, in front of the name. For example, the identifier __spam
occurring in a class named Ham will be transformed to _Ham__spam
. This transformation is independent of the syntactical context in which the identifier is used.
我已经阅读了所有关于此主题的类似帖子,但 none 我发现它们与我的问题相关,帮助我弄清楚发生了什么。
class A:
def __init__(self, value):
self.__value = value
self.__conn = httpsconnection # Specifics don't matter
class B(A):
def __init__(self, id, type, value):
super().__init__(value)
self.data = self.__create_sub_class(id, type)
def __create_sub_class(self, id, type):
self.__conn.request(...)
...
return data
class C(B):
def __init__(self, id, value):
super().__init__(id, externalVariable, value)
我收到的错误是 AttributeError: 'C' object has no attribute '_B__conn'
class C
不应该从 B
继承变量,而 B
是从 A
继承变量吗?
如果您想在子classes 中使用这些名称,请不要使用前导双下划线名称。 __name
明确设计为使名称 class 私有 ,即仅对确切的 class 有用。这个想法是,您可以在框架中使用此类名称,而无需限制 subclasses 可以使用的名称。
这样的名字在编译时被破坏;他们有 _ClassName
前缀(使用当前的 class 名称)。如果您想表明名称是内部名称,只需使用单下划线名称即可(Python 没有实际的隐私模型,名称始终可访问):
class A:
def __init__(self, value):
self._value = value
self._conn = httpsconnection # Specifics don't matter
class B(A):
def __init__(self, id, type, value):
super().__init__(value)
self.data = self._create_sub_class(id, type)
def _create_sub_class(self, id, type):
self._conn.request(...)
...
return data
参见词法分析文档中的Reserved classes of identifiers:
__*
Class-private names. Names in this category, when used within the context of a class definition, are re-written to use a mangled form to help avoid name clashes between “private” attributes of base and derived classes.
和引用的 documentation on names:
Private name mangling: When an identifier that textually occurs in a class definition begins with two or more underscore characters and does not end in two or more underscores, it is considered a private name of that class. Private names are transformed to a longer form before code is generated for them. The transformation inserts the class name, with leading underscores removed and a single underscore inserted, in front of the name. For example, the identifier
__spam
occurring in a class named Ham will be transformed to_Ham__spam
. This transformation is independent of the syntactical context in which the identifier is used.