Giter VIP home page Giter VIP logo

Comments (4)

chfw avatar chfw commented on July 21, 2024

个人看法!

抽象类在Java,C++有用。在Python里,我们讲 duck typing,只要你有对应的功能,就是我类。

所以在Python里,抽象类几乎都是浪费金钱和时间的方式。要真有时间,用心写测试和文档,回报率更高!

from one-python-craftsman.

piglei avatar piglei commented on July 21, 2024

你好 @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.

chfw avatar chfw commented on July 21, 2024

再补充一下:

很多人来自OOP背景的开发人员,总是不能够意识到 Python 也是函数编程语言。万事必用类!到了无类无法写程序的地步。这是一个盲点。

另一个盲点: Python 支持的是动态类型 dynamic typing. 用虚拟类来控制子类,是需要自己写的,Python并不支持全部 OOP.

from one-python-craftsman.

Fatalerr avatar Fatalerr commented on July 21, 2024

@piglei @chfw 解释的非常清楚了,谢谢两位!

from one-python-craftsman.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.