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

Peng Yu pengyu.ut at gmail.com
Tue Jun 22 11:53:25 EDT 2010


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)

$ ./main.py
Traceback (most recent call last):
  File "./main.py", line 10, in <module>
    print inspect.getsource(a)
  File "/home/pengy/utility/linux/opt/Python-2.6.5/lib/python2.6/inspect.py",
line 689, in getsource
    lines, lnum = getsourcelines(object)
  File "/home/pengy/utility/linux/opt/Python-2.6.5/lib/python2.6/inspect.py",
line 678, in getsourcelines
    lines, lnum = findsource(object)
  File "/home/pengy/utility/linux/opt/Python-2.6.5/lib/python2.6/inspect.py",
line 519, in findsource
    file = getsourcefile(object) or getfile(object)
  File "/home/pengy/utility/linux/opt/Python-2.6.5/lib/python2.6/inspect.py",
line 441, in getsourcefile
    filename = getfile(object)
  File "/home/pengy/utility/linux/opt/Python-2.6.5/lib/python2.6/inspect.py",
line 418, in getfile
    raise TypeError('arg is not a module, class, method, '
TypeError: arg is not a module, class, method, function, traceback,
frame, or code object


-- 
Regards,
Peng



More information about the Python-list mailing list