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

P

Peng Yu

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top