Find out which module a class came from

Rick Johnson rantingrickjohnson at gmail.com
Fri Apr 7 21:00:48 EDT 2017


On Friday, April 7, 2017 at 7:49:11 PM UTC-5, Steve D'Aprano wrote:
> But for merely ordinary obfuscation caused by poor design,
> your best bet is probably to inspect Foo.__module__.
> 
> You can also try:
> 
> inspect.getsource(FooClass)
> inspect.getsourcefile(FooClass)

Hmm, I tried that code but all i got was a nameError. 

    >>> inspect.getsource(FooClass)
    
    Traceback (most recent call last):
      File "<pyshell#0>", line 1, in <module>
        inspect.getsource(FooClass)
    NameError: name 'inspect' is not defined

    >>> inspect.getsourcefile(FooClass)

    Traceback (most recent call last):
      File "<pyshell#1>", line 1, in <module>
        inspect.getsourcefile(FooClass)
    NameError: name 'inspect' is not defined

You must be using the Python version that has batteries _and_
imports included.




More information about the Python-list mailing list