Is possible to know the path of the file of a subclass in python?
I have a plugin system. The plugins subclass from a common ancestor... ad lo开发者_运维问答ok like this:
-- SDK
--- basePlugin.py
-- PLUGINS
--- PluginA
---- Plugin.py
---- Config.ini
--- PluginB
---- Plugin.py
---- Config.ini
I need to read the info of Config.ini
in basePlugin.py __init__
. CUrrently in each plugin I do:
class PluginA(BaseSync):
__init__(self, path):
super(PluginA,self).__init__(self, __file__)
But wonder if is possible to know in the parent class in which file is located the sub-class...
Assuming BaseSync
is a new-style class, the parent class BaseSync
could find the file that defines PluginA
this way:
import sys
class BaseSync(object):
def __init__(self):
path=sys.modules[self.__module__].__file__
(so you don't have to pass the path
explicitly).
精彩评论