Example function:
def my_function():if 9>8 print("Hello from a function") else print("Goodbye")
Expected Output:
String : """def my_function(): if 9>8 print("Hello from a function") else print("Goodbye")"""
Includes the reserved words (def,if,else) as well.
martineau
124k29 gold badges181 silver badges319 bronze badges
-
1The definition of your example function is syntactically invalid.martineau– martineau2021年10月21日 08:41:35 +00:00Commented Oct 21, 2021 at 8:41
1 Answer 1
Use inspect.getsource()
As pointed out by @martineau (thx), your function had a SyntaxError. I reformatted your function and used a ternary to keep it single-line.
import inspect
def my_function(): print("Hello from a function") if 9>8 else print("Goodbye")
print(inspect.getsource(my_function))
Output:
def my_function(): if 9>8 print("Hello from a function") else print("Goodbye")
answered Oct 21, 2021 at 7:33
milt_on
5571 gold badge10 silver badges21 bronze badges
Sign up to request clarification or add additional context in comments.
lang-py