Python – Given a Python class, how can I inspect and find the place in the code where it is defined

python

I'm building a debugging tool.

IPython lets me do stuff like

MyCls??

And it will show me the source.

Best Answer

sys.modules[MyCls.__module__].__file__

or

inspect.getsourcefile(MyCls)

There are more __xxx__ attributes on various objects you might find useful.