Can a function return a representation of what was passed?

Viewed 61

Example for clarity:

def foo(arg1):
   <function goes here>

x = 5
foo(x > 4)

# foo() should return "x > 4" here.
# I do not want it to return the value, True.

It's for a custom file debugger and I was wondering if it was a possibility.
If not, is there a way for a variable outside of a function's scope be passed into it? (the actual variable, not its value.)

1 Answers

It's not as entirely impossible as the comments area says. You may need to learn some built-in libraries of Python, such as tokenize, to get the expression you want or something else by getting the source code that Python is executing.

It's not easy to realize this function. In fact, I don't know how to do it. Here are just some ideas for you. I think you need to try after you have a good foundation of Python.

If you are familiar with Chinese, you can refer to this video. I'm sorry I can't provide references in other languages now: How to know what the parameter is before it is passed in the function?

There is the GitHub warehouse mentioned in the video: objprint.

Related