Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python functions that can modify their own input

Tags:

python

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?

like image 404
Ben Lerner Avatar asked Dec 26 '22 15:12

Ben Lerner


1 Answers

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.

like image 140
arshajii Avatar answered Dec 31 '22 12:12

arshajii