抽象基类或打字。支持__STR______________
是否有用于在子类中实现的__ str __
的内置ABC?还是打字协议?
我想要一个仅接受__ str __
__ HASH __
和__ eq __
的功能。我找到了Hashable
,但不是Stringable
Is there a builtin ABC for enforcing __str__
to be implemented in subclasses? Or a typing protocol?
I want a function that only accepts classes with __str__
__hash__
and __eq__
. I've found Hashable
but not Stringable
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有此类内置
ABC
。实际上,每个班级都有此方法从object
:请参阅 docs 。
There is no such built-in
ABC
. In fact, every class has this method inherited fromobject
:See docs.
没有这样的内置抽象类,但是您可以执行这些要求。
另外,您可以在运行时检查特定的结构性子类型,以获取平等,并返回
typeError
如果不是这样(但可能不是最佳实践):There's no such a builtin abstract class, but you can enforce those requirements.
Also, you could check a specific structural subtyping for equality at runtime, and return a
TypeError
if it's not the case (but it may not be a best practice):