1

I was wondering if there is a way to print what the funciton definition?

so if I want to do something like:

def hello():
    print 'hello'

some_function_to_show_definition(hello())

and the output should be:

print 'hello'

Just messing around in python and I was just wondering :)

1

1 Answer 1

3

inspect is the way to go:

In [8]: def foo():
   ...:     print 'hello'
   ...:     

In [9]: import inspect

In [10]: inspect.getsourcelines(foo)
Out[10]: ([u'def foo():\n', u"    print 'hello'\n"], 1)
Sign up to request clarification or add additional context in comments.

2 Comments

You just beat me to it! Just a note, this does not work in CPython interactive mode.
Yep, it's probably not a good idea if a program relies on such kind of low level introspection.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.