【发布时间】:2018-02-11 20:55:29
【问题描述】:
我已阅读有关此主题的所有类似帖子,但我发现没有一篇与我的问题相关,有助于我弄清楚发生了什么。
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'
不应该类C 继承来自B 的变量,后者从A 继承它?
【问题讨论】:
-
为什么要使用双下划线名称?这些是为了避免可继承性而明确设计的。
-
@MartijnPieters 它们是我的私有变量。我在学习编程时用另一种语言养成的习惯。
-
@Spedwards:除了类私有名称之外,Python 中没有隐私模型,而且您的用例不符合他们的目标。
标签: python python-3.x class subclass