Is it possible to print a function as a string in Python? [duplicate]



<strong>Possible Duplicate:</strong><br /><a href="https://stackoverflow.com/questions/1562759/can-python-print-a-function-definition" rel="nofollow">Can Python print a function definition?</a>


In Javascript, it is possible to print a function's code as a string?

Example in Javascript:

function thisFunctionPrintsItself(){ return thisFunctionPrintsItself.toString(); }

Is it possible to do the same in Python?


You can do it, but the result isn't useful since everything is compiled to bytecode first.

def printItself(): print repr(printItself.func_code.co_code)

You can also use the dis module for disassembly, but the results aren't garuenteed to be portable.

def disassembleItself(): print __import__('dis').dis(disassembleItself)


def foo (): import inspect return inspect.getsource(foo) print (foo())

Here, the inspect module reads the source files, so it won't work if they're missing (and just the .pyc or .pyo modules are being used) or the function was compiled on-the-fly, in the interactive interpreter or otherwise.


  • python popen special characters not parsing file
  • PyCharm ValueError: failed to parse CPython sys.version
  • python - format float to decimal in pyQt
  • Declaring a class and subclass in two different files in python
  • flatMap on a sequence of tuples
  • Labelled Generic containing `$eq`
  • Is it possible to print a function as a string in Python? [duplicate]
  • python file I/O with binary data
  • Pandas: cannot import name adjoin
  • Java runtime retention annotations - annotation class required at compile time but not at runtime?
  • Is there a built in python function to escape a string of unicode characters into unicode escape seq
  • Strange issue with AES CTR mode with Python and Javascript
  • PHP shell_exec running a shellscript with ssh
  • Lucene Query Boosting
  • Save SQL to YAML as is
  • Mocking package private classes
  • Multiple dex files define Lcom/google/android/gms/common/internal/zzag
  • Unpickling mid-stream (python)
  • Java method invocation order
  • Converting a PEM private key file to a JAVA PrivateKey Object
  • Dereferenceing on casting the void pointer to float*/int*
  • Why does the address of an object change across methods?
  • Doc2vec : TaggedLineDocument()
  • 'include' of functions in groovy scripts
  • how do you obtain the address of an instance after overriding the __str__ method in python
  • Using $compile in a directive triggers AngularJS infinite digest error
  • Grails calculated field in SQL
  • x64 applications using gdi+: what are the consequences on performance?
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • Is there a perl module to validate passwords stored in “{crypt}hashedpassword” “{ssha}hashedpassword
  • Meteor helpers not available in Angular template
  • Why is the size of this struct 32?
  • How to add date and time under each post in guestbook in google app engine
  • JSON with duplicate key names losing information when parsed
  • Return words with double consecutive letters
  • php design question - will a Helper help here?
  • AngularJs get employee from factory
  • using HTMLImports.whenReady not working in chrome
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4