设置:Python 3.3
我有一个名为 SourceBase 的基类,它定义了抽象方法和值:
import abc
class SourceBase(object):
__metaclass__=abc.ABCMeta
pluginid='undefined' #OVERRIDE THIS IN YOUR SUBCLASS. If you don't, the program will ignore your plugin.
@abc.abstractmethod
def get_images(self):
'''This method should return a list of URLs.'''
return
@abc.abstractmethod
def get_source_info(self):
'''This method should return a list containing a human friendly name at index 0, and a human readable url describing the source for this repository.
For example, the EarthPorn subreddit returns a list ['EarthPorn Subreddit', 'http://reddit.com/r/EarthPorn'].
This is used to populate the treeview object with your source information.'''
return
@abc.abstractmethod
def get_pluginid(self):
'''This method should return a string that represents this plugins ID.
The pluginid is used to make calls to this plugin when necessary. It should be unique as ids are in a shared pool,
so make sure the id is unique. The id should remain the same even when updated as some settings with the pluginid
are persisted by the main application, and they will be lost if the id changes.
'''
return
这是我写的一些python插件的父类(super class),是它的子类。它们在运行时动态加载,并且所有这些都有效,除了即使我向我的 SourceBase 添加了一个新的抽象方法,插件仍然加载。他们不应该,因为他们都没有我的新方法。 (我给了它 @abc.abstractmethod 标记)。
我的 google-fu 并没有真正显示任何东西,所以我不确定为什么我仍然可以实例化这些插件,即使父类(super class)说它们是抽象的。
比如在SourceBase中,我添加了:
@abc.abstractmethod
def get_dependencies(self):
print('ERROR: THIS PLUGIN SHOULD NOT HAVE LOADED.')
'''This method should return a list of package names. The host program will check if these packages are available.
If they are not available, the plugin will be disabled, but the user will be able to elect to install these packages.'''
return
我没有在我的插件中定义这个方法,但我仍然在终端上得到这个输出:
....
Screen Height:1080
Screen Width:1920
files: ['bingIOTD.py', 'redditEP.py', 'redditLP.py', '__init__.py']
ERROR: THIS PLUGIN SHOULD NOT HAVE LOADED. <--abstract method
我不确定为什么它会忽略它,我是不是漏掉了什么...?我以前用没有动态加载的普通类做过。任何帮助表示赞赏。我知道我可能可以做一个解决方法(进行默认返回,检查),但这似乎不是正确的方法。
如果您需要更多源代码,我的项目在 SourceForge 上 here.
请您参考如下方法:
In Python3元类由
指定class SourceBase(metaclass=abc.ABCMeta):
不是
class SourceBase(object):
__metaclass__=abc.ABCMeta
代码忽略了抽象方法装饰器,因为就 Python3 而言,SourceBase 只是一个带有属性
<的标准类(
type 的实例)/em> name
__metaclass__ 而不是
abc.ABCMeta 的实例。
的标准类(
