Comments (4)
个人看法!
抽象类在Java,C++有用。在Python里,我们讲 duck typing,只要你有对应的功能,就是我类。
所以在Python里,抽象类几乎都是浪费金钱和时间的方式。要真有时间,用心写测试和文档,回报率更高!
from one-python-craftsman.
你好 @Fatalerr ,抽象类的独特之处在我看来主要是两点。
第一点,抽象类无法被直接实例化使用,并且当它的子类在继承抽象类时被附加了一层额外限制,比如必须 override 某些抽象方法。这在某些场景下提供了一些 (可能并无大用) 的限制。
第二点就更重要一点。相比普通类,抽象类提供了一种更松散的子类化机制。对于普通类来说,A 必须显式从继承于 B,B 才能算是 A 的子类。但抽象类不一样。
首先,通过调用 A.register(B)
方法可以在不修改 B 的父类的情况下将 B 注册成抽象类 A 的子类。
import abc
class AbstractFoo(metaclass=abc.ABCMeta):
pass
class Bar():
pass
# output: False
print(isinstance(Bar(), AbstractFoo))
AbstractFoo.register(Bar)
# output: True
print(isinstance(Bar(), AbstractFoo))
其次,抽象类还有一个更有意思的东西叫 abc.ABCMeta.subclasshook ,通过在抽象类里定义这个钩子,我们可以根本性的改变抽象类在进行子类化判断的行为。
collections.abc
模块里的很多抽象类就是很好的例子:
class Foo():
pass
class Bar():
def __next__(self):
return 1
def __iter__(self):
return self
# output: False
print(isinstance(Foo(), Iterable))
# output: True
# 只要定义了 __iter__,就是 iterable 的子类
print(isinstance(Bar(), Iterable))
总结一下,抽象类提供的最大特点就两个:强制要求继承时的重载范围 和 更松散的子类化机制。对于日常编程的大多数场景来说,这两点好处可能都无关紧要,所以一般我们都不会去写抽象类。
但是,如果你刚好在搞一些比较复杂的框架或者通用库之类的开发,并且这个东西又刚好和类型系统息息相关 (考虑 collections.abc 场景) ,那么抽象类的第二个特点可能会派上用场。
from one-python-craftsman.
再补充一下:
很多人来自OOP背景的开发人员,总是不能够意识到 Python 也是函数编程语言。万事必用类!到了无类无法写程序的地步。这是一个盲点。
另一个盲点: Python 支持的是动态类型 dynamic typing. 用虚拟类来控制子类,是需要自己写的,Python并不支持全部 OOP.
from one-python-craftsman.
from one-python-craftsman.
Related Issues (20)
- blog服务好像挂了.https://www.zlovezl.cn/ HOT 5
- Python suffixed of some code block is missing HOT 1
- To add software craftsmanship manifesto?
- 縮進問題 HOT 1
- 微信公众号图裂了 HOT 1
- 使用namedtuple可以用解包赋值给多个变量 HOT 1
- 推荐添加用在装饰器上的自省函数 inspect.signature 的使用说明 HOT 1
- 第六篇文章“Write Cleaner Python: Use Exceptions”链接已失效 HOT 1
- 现已支持switch case HOT 2
- 关于「使用 try/while/for 中 else 分支」 HOT 1
- 写的很棒 HOT 1
- OOP 原则 HOT 2
- 关于第三章里【常见误区】- 【字符串拼接并不慢】 HOT 1
- 书籍建议 HOT 3
- 有关Monkey Patch HOT 1
- 是否有epub电子书购买渠道? HOT 2
- On Code Repo HOT 2
- On Code in 10-a-good-player-know-the-rules.md HOT 1
- 类型注解来声明类型 HOT 1
- 关于异常处理代码对照示例的疑问
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from one-python-craftsman.