你会看到有时是以下:
class Abstract1( object ):
"""Some description that tells you it's abstract,
often listing the methods you're expected to supply."""
def aMethod( self ):
raise NotImplementedError( "Should have implemented this" )
因为Python没有(并且不需要)一个正式的接口契约,抽象和接口之间的Java风格的区别不存在。如果有人通过努力来定义一个正式的接口,它也将是一个抽象类。唯一的区别是在docstring中声明的意图。
抽象和接口之间的区别是一个hairsplitting的东西,当你有鸭打字。
Java使用接口,因为它没有多重继承。
因为Python有多重继承,你也可能看到这样的东西
class SomeAbstraction( object ):
pass # lots of stuff - but missing something
class Mixin1( object ):
def something( self ):
pass # one implementation
class Mixin2( object ):
def something( self ):
pass # another
class Concrete1( SomeAbstraction, Mixin1 ):
pass
class Concrete2( SomeAbstraction, Mixin2 ):
pass
这使用一种带有mixins的抽象超类来创建不相交的具体子类。