why i can't get the sourcecode with inspect module?
Dave Angel
davea at davea.name
Wed Aug 20 09:06:15 EDT 2014
luofeiyu <elearn2014 at gmail.com> Wrote in message:
> >>> import inspect
> >>> def changer(x,y):
> ... return(x+y)
> ...
> >>> dir()
> ['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__
> 'changer', 'inspect']
> >>> inspect.getsource(changer)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "D:\Python34\lib\inspect.py", line 830, in getsource
> lines, lnum = getsourcelines(object)
> File "D:\Python34\lib\inspect.py", line 819, in getsourcelines
> lines, lnum = findsource(object)
> File "D:\Python34\lib\inspect.py", line 667, in findsource
> raise OSError('could not get source code')
> OSError: could not get source code
> >>>
>
To put it simply, inspect can get the source if it's in python,
and if it's in a file visible on your own system from the same
location it was built from.
Since your function was built interactively, inspect cannot find
any source.
--
DaveA
More information about the Python-list
mailing list