相关疑难解决方法(0)

Python中私有和受保护方法的继承

我知道,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'
Run Code Online (Sandbox Code Playgroud)

那么,这种行为是否意味着,"受保护"方法将被继承,但"私有"根本不会被继承?
或者我错过了什么?

python methods inheritance private protected

58
推荐指数
4
解决办法
4万
查看次数

标签 统计

inheritance ×1

methods ×1

private ×1

protected ×1

python ×1