from abc import ABC, abstractmethod
class AbstractCrawler(ABC):
@abstractmethod
def get_playlist_videos(self):
pass
@abstractmethod
def get_related_videos(self):
pass
@abstractmethod
def create_playlists(self):
pass
然后:
class ImplCrawler(AbstractCrawler):
pass
>>> i = ImplCrawler()
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: Can't instantiate abstract class ImplCrawler with abstract methods create_playlists, get_playlist_videos, get_related_videos
与之相比:
class ImplCrawler(AbstractCrawler):
def get_playlist_videos(self):
pass
def get_related_videos(self):
pass
def create_playlists(self):
pass
>>> i = ImplCrawler()
# No error
您可以将类及其方法标记为abstract:
然后:
与之相比:
相关问题 更多 >
编程相关推荐