python抽象基类的作用_为什么在Python中使用抽象基类?

@奇怪的回答是正确的,但我认为它错过了

真实的

,

实际的

为什么在鸭子打字的世界里巨蟒有ABC?

抽象方法是整洁的,但在我看来,它们并不能真正填充任何尚未被duck类型所覆盖的用例。抽象基类的真正力量在于

the way they allow you to customise the behaviour of

isinstance

and

issubclass

. (

__subclasshook__

基本上是比Python更友好的API

__instancecheck__

and

__subclasscheck__

钩子。)调整内置结构以处理自定义类型是Python哲学的一部分。

Python的源代码是示例性的。

Here

是怎样

collections.Container

在标准库中定义(编写时):

class Container(metaclass=ABCMeta):

__slots__ = ()

@abstractmethod

def __contains__(self, x):

return False

@classmethod

def __subclasshook__(cls, C):

if cls is Container:

if any("__contains__" in B.__dict__ for B in C.__mro__):

return True

return NotImplemented

这个定义

_子章节__

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值