python - Getting the module file path of a derived class via inheritance -
suppose have following:
$ more a.py import os class a(object): def getfile(self): return os.path.abspath(__file__)
-
$ more b.py import class b(a.a): pass
-
>>> import b >>> x=b.b() >>> x.getfile() '/users/sbo/tmp/file/a.py'
this clear. no surprise code. suppose want x.getfile() return path of b.py without having define copy of getfile() under class b.
i did this
import os import inspect class a(object): def getfile(self): return os.path.abspath(inspect.getfile(self.__class__))
i wondering if there's strategy (and in case, want write here can useful others) or potential issues solution present.
cw it's more discussion question, or yes/no kind of question
sys.modules[self.__class__.__module__].__file__
Comments
Post a Comment