Domanda

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 :)

È stato utile?

Soluzione

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)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top