从Python的子类中删除属性

MRo*_*lin 3 python inheritance

有什么方法可以从父类中存在的子类中删除属性?

在下面的例子中

class A(object):
    foo = 1
    bar = 2

class B(A):
    pass

# <desired code here>

b = B()
assert hasattr(b, 'bar') == False
Run Code Online (Sandbox Code Playgroud)

我们可以编写任何代码来使断言通过吗?

jam*_*lak 5

class A(object):
    foo = 1
    bar = 2


class B(A):
    @property
    def bar(self):
        raise AttributeError


>>> b = B()
>>> b.bar

Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    b.bar
  File "<pyshell#15>", line 4, in bar
    raise AttributeError
AttributeError
Run Code Online (Sandbox Code Playgroud)