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

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -