Python 继承 - 如何禁用函数
在 C++ 中,您可以通过在子类中将其声明为私有来禁用父类中的函数。 在 Python 中如何做到这一点? IE 如何从子级的公共界面隐藏父级的功能?
In C++ you can disable a function in parent's class by declaring it as private in the child class. How can this be done in Python? I.E. How can I hide parent's function from child's public interface?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Python 中确实不存在任何真正的“私有”属性或方法。 您可以做的一件事就是简单地重写子类中不需要的方法,并引发异常:
There really aren't any true "private" attributes or methods in Python. One thing you can do is simply override the method you don't want in the subclass, and raise an exception:
kurosch 解决问题的方法不太正确,因为您仍然可以使用
b.foo
而不会出现AttributeError
。 如果不调用该函数,则不会发生错误。 我可以想到以下两种方法来实现此目的:Bar 使用“wrap”模式来限制对包装对象的访问。 Martelli 有一个很好的演讲来处理这个问题。 Baz 使用内置属性来实现描述符协议要覆盖的属性。
kurosch's method of solving the problem isn't quite correct, because you can still use
b.foo
without getting anAttributeError
. If you don't invoke the function, no error occurs. Here are two ways that I can think to do this:Bar uses the "wrap" pattern to restrict access to the wrapped object. Martelli has a good talk dealing with this. Baz uses the property built-in to implement the descriptor protocol for the attribute to override.
kurosch 答案的一个变体:
这会在属性上引发一个 AttributeError ,而不是在调用方法时引发。
我本来会在评论中建议它,但不幸的是还没有它的声誉。
A variation on the answer of kurosch:
This raises an
AttributeError
on the property instead of when the method is called.I would have suggested it in a comment but unfortunately do not have the reputation for it yet.
这可能会导致抛出一些令人讨厌且难以发现的异常,因此您可以尝试以下操作:
This may lead to some nasty and hard to find exceptions being thrown though, so you might try this:
那可能更简单。
That could be even simpler.
这是我知道的最干净的方法。
重写这些方法并让每个被重写的方法调用您的disabledmethods() 方法。 像这样:
This is the cleanest way I know to do it.
Override the methods and have each of the overridden methods call your disabledmethods() method. Like this:
另一种方法是定义一个在访问时出错的描述符。
这本质上与上面一些人使用的财产方法类似。 但它的优点是,如果该函数确实不存在,
hasattr(Bar, 'foo')
将返回False
,正如人们所期望的那样。 这进一步减少了出现奇怪错误的可能性。 尽管它仍然显示在dir(Bar)
中。如果您对它的作用及其工作原理感兴趣,请查看数据模型页面的描述符部分 https://docs.python.org/3/reference/datamodel.html#descriptors 以及描述符如何https://docs.python.org/3/howto/descriptor.html
Another approach is define an descriptor that errors on access.
This is similar in nature to the property approach a few people have used above. However it has the advantage that
hasattr(Bar, 'foo')
will returnFalse
as one would expect if the function really didn't exist. Which further reduces the chance of weird bugs. Although it does still show up indir(Bar)
.If you are interested in what this is doing and why it works check out the descriptor section of the data model page https://docs.python.org/3/reference/datamodel.html#descriptors and the descriptor how to https://docs.python.org/3/howto/descriptor.html