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

James Mills prologic at shortcircuit.net.au
Tue Jun 22 12:00:15 EDT 2010


On Wed, Jun 23, 2010 at 1:53 AM, Peng Yu <pengyu.ut at gmail.com> wrote:
> 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?

If you "read" the documentation carefully:

"""
   getsource(object)
        Return the text of the source code for an object.

        The argument may be a module, class, method, function, traceback, frame,
        or code object.  The source code is returned as a single string.  An
        IOError is raised if the source code cannot be retrieved.
"""

This "does not" include an object / instance (whichever you prefer).

$ python main.py
class A:
 pass


$ cat main.py
import inspect

class A:
 pass

a=A()

print inspect.getsource(a.__class__)

cheers
James

-- 
--
-- "Problems are solved by method"



More information about the Python-list mailing list