Find out which module a class came from

Chris Angelico rosuav at gmail.com
Fri Apr 7 16:31:09 EDT 2017


On Sat, Apr 8, 2017 at 6:24 AM, Tobiah <toby at tobiah.org> wrote:
> I was viewing the python source for a program at work and
> came across a class name that I knew my company had written:
>
>         import mycmp1
>         import mycmp2
>         import mycmp3
>         import mycmp4
>         import mycmp5
>
>         foo = FooClass()
>
>
> So I knew that FooClass was defined in one of those imports, but
> I thought it would be tedious to track down the location of all
> of those modules (is module.__file___ the best way) and scan them
> for the class definition.  Is there a better way to find the
> definition of FooClass()?

If they're just "import modulename", then FooClass won't be defined
unless it's been injected into the builtins or something. Are they
"from mycmp1 import *"?

Normally, you should be able to just look at FooClass.__module__ to
see where it's been created. But if your module layout is messy, that
might not be sufficient information.

ChrisA



More information about the Python-list mailing list