4

I want to create a Python function that can inspect its own input, rather than the output of its input. For example, a function raw_str that returns its input exactly, as a string:

>>> raw_str(2+2)
'2+2'

rather than:

>>> str(2+2)
'4'

Is there any way to do this?

2
  • 1
    Is raw_str('2+2') acceptable? Otherwise, @JBernardo is right. Commented Oct 28, 2012 at 2:49
  • In Ruby this is sometimes done by the library implementing such a function parsing the source of the call-site and providing the function output based on such a scan. I'm not sure how well such an approach would work in Python. Commented Oct 28, 2012 at 2:52

1 Answer 1

9

This is not possible because the arguments are evaluated before they are passed on to the function - so there will be no way to distinguish between 2 + 2 and 3 + 1 (for instance) within the function body. Without more context, it's hard to suggest possible solutions to the problem.

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.