Finding source from class or instance?
Emile van Sebille
emile at fenx.com
Wed Jan 17 20:27:44 EST 2001
An instance has access to a __class__ attribute, and a class
to a module attribute, and sys.modules holds the file name
in which the class was defined.
srceClass = inst.__class__
srceModule = srceClass.__module__
sys.modules[srceModule].__file__
or in one step from an instance:
sys.modules[inst.__class__.__module__].__file__
That gets you to the file anyway. If you're so inclined,
you can then open, read and extract the code for the class.
HTH,
--
Emile van Sebille
emile at fenx.com
-------------------
"Stuart Stanley" <stuarts at 171.69.180.197> wrote in message
news:979777210.145620 at sj-nntpcache-5...
>
>
> Folks,
>
> Is there a (easy!) way to get the source code from a class
or instance
> of a class? A preference would lie in just getting the
source of the
> base class itself, but I would settle for getting the file
name where
> the base class lives in. I figure the information is
sitting in the
> modules somewhere, but I don't know how to navigate there
to get it....
>
> Thanks! --stuart
>
>
> --
> "Computer Science is no more about computers than |
Stuart Stanley
> astronomy is about telescopes." - E. W. Dijkstra |
stuarts at cisco.com
> | Cisco
Systems, inc
> | St
Paul, MN
> |
612.578.6412
More information about the Python-list
mailing list