2

i am implementing a function framework in python to be accessed from C#.

As a requirement, i have to provide a "string representation" of the function at runtime.

Is there a way to get the code definition of a function as a string? something like:

def sum(a,b): 
    return a + b

def ToString(sum):
    # would return "def sum(a,b): return a+b" or something alike.

Thank you very much!

1
  • Does the representation actually have to indicate what the source code of the function is? What are you going to do with the representation? Commented Mar 16, 2012 at 22:41

1 Answer 1

1

The function you are looking for is inspect.getsource(). Note that it will only work if the source file is accessible, so it won't work in the interactive console.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.