Why inspect.getsource() can not getsource for a class?

Ian Kelly ian.g.kelly at gmail.com
Tue Jun 22 12:01:34 EDT 2010


On Tue, Jun 22, 2010 at 9:53 AM, Peng Yu <pengyu.ut at gmail.com> wrote:
> Hi,
>
> It seems I don't completely understand how getsource works, as I
> expect that I should get the source code of class A. But I don't.
> Would you please let me know what I am wrong?
>
> $ cat main.py
> #!/usr/bin/env python
>
> import inspect
>
> class A:
>  pass
>
> a=A()
>
> print inspect.getsource(a)

You passed in an instance of the class, not the class itself.  The
following will work:

print inspect.getsource(A)

As will:

print inspect.getsource(a.__class__)

Cheers,
Ian



More information about the Python-list mailing list