【发布时间】:2019-02-10 06:57:27
【问题描述】:
我知道,Python 中没有“真正的”私有/受保护方法。这种方法并不是要隐藏任何东西。我只是想了解 Python 是做什么的。
class Parent(object):
def _protected(self):
pass
def __private(self):
pass
class Child(Parent):
def foo(self):
self._protected() # This works
def bar(self):
self.__private() # This doesn't work, I get a AttributeError:
# 'Child' object has no attribute '_Child__private'
那么,这种行为是否意味着“受保护”方法将被继承而“私有”方法根本不会被继承?
还是我错过了什么?
【问题讨论】:
-
“这不起作用”是什么意思?
-
我编辑了原始帖子。
-
你必须这样称呼它,假设 c 是 Child
c._Parent__private()的一个实例 -
它没有按应有的方式工作吗? AFAIK 私有方法不会被继承。 stackoverflow.com/questions/8241462/…
标签: python inheritance methods private protected