@
practicer 嗯,我也看了这本书,文档中有这样一段话:
@
abc.abstractmethod
A decorator indicating abstract methods.
Using this decorator requires that the class ’ s metaclass is ABCMeta or is derived from it.
但是如下代码运行没有问题:
```python
from abc import ABCMeta, abstractmethod
class Shape(object):
@
abstractmethod def area(self):
pass
def __lt__(self, obj):
return self.area() < obj.area()
def __eq__(self, obj):
return self.area() == obj.area()
class Rectangle(Shape):
def __init__(self, w, h):
self.w = w
self.h = h
def area(self):
return self.w * self.h
if __name__ == "__main__":
r1 = Rectangle(2, 3)
r2 = Rectangle(2, 4)
print(r1 < r2)
```
如下代码也没有问题
``` python
from abc import ABCMeta, abstractmethod
class Shape(metaclass=ABCMeta):
@
abstractmethod def area(self):
pass
def __lt__(self, obj):
return self.area() < obj.area()
def __eq__(self, obj):
return self.area() == obj.area()
class Rectangle(Shape):
def __init__(self, w, h):
self.w = w
self.h = h
def area(self):
return self.w * self.h
if __name__ == "__main__":
r1 = Rectangle(2, 3)
r2 = Rectangle(2, 4)
print(r1 < r2)
```