转载 于http://blog.csdn.net/lis_12/article/details/53842299
抽象基类
要定义抽象基类,需要使用abc模块。该模块定义了一个元类(ABCMeta)和一组装饰器@abstractmethod,@abstractproperty
。可以按如下方式使用
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
要定义抽象类,需要将元类设置为ABCMeta
。这一步是必须的,因为抽象类的实现离不开元类。在抽象类中,@abstractmethod和@abstractproperty
装饰的方法/特性其子类必须实现。
抽象类并不能直接实例化。
- 1
- 1
这一限制也适用于派生类。如果类Foo1继承了Foo,但是Foo1没有实现Foo中的抽象方法,当创建Foo1的实例时,会生成类似的错误。
- 1
- 2
- 3
- 1
- 2
- 3
尽管抽象类会在实现的方法和特性上强制实施规则,但它不会对参数和返回值进行一致性检查。所以,抽象类不会检查子类是否使用了与抽象方法相同的参数和返回值。
虽然抽象类无法实例化,但是它可以决定子类中必须实现的方法和特性。而且,基类中的抽象方法/特性仍然可以从子类中调用。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
类注册
抽象基类支持对已经存在的类进行注册,使其属于该基类,使用register()进行注册。
如向抽象基类A注册类B,
A.register(B)
。
向抽象基类注册某个类时,对于注册类的实例,涉及抽象基类的类型检查操作(isinstance,issubclass)将返回True。
向抽象类注册某个类时,不会检查该类是否实现了抽象方法/特性。这种注册只影响类型检查,它不会向注册的类进行额外的错误检查。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
Python的内置类型组织到一个相对扁平的层次结构中。如int,float,可以看到他们直接继承自所有对象的根(object),而不是表示数字的中间基类。因此很难编写程序来根据类型检查和操作对象。抽象类机制解决了这一难题,它允许将已存在的对象组织到用户定义的类型层次结构中。