0

I want to write a function that uses a given argument inside the same function in two ways:

  1. as a string
  2. as a object, int or float, etc.

the structure should be like this:

def func(any_given_argument):
    print('the "any_given_argument" value is:', any_given_argument)

Examples of desired result:

func(2) = 'the 2 value is:' 2
func(df['col1']) = "the value of df['col'] is:" value of df['col1'] series
func(2+4) = 'the value of 2+4 is:' 6

Need some help doing this because I've tried a lot but didn't succeed... thanks a lot in advance!!

Barmar
  • 741,623
  • 53
  • 500
  • 612
Sandro S
  • 1
  • 1
  • 2
    This isn't possible. Functions only receive the value of their argument, there's no way to get the original expression that was used. The compiler doesn't save the original expression text anywhere. – Barmar Apr 04 '23 at 17:23
  • 3
    This smells like XY problem and is indication that probably something is wrong with your design. The names used in the code shouldn't matter. Check also [Keep data out of your variable names](https://nedbatchelder.com/blog/201112/keep_data_out_of_your_variable_names.html) – buran Apr 04 '23 at 17:31
  • What do you mean "as a string"? You want a string that represents the *expression* that was passed as an argument? There is no reasonable way to accomplish that, and you almost certainly don't need to do that. Why don't you tell us what you are actually trying to accomplish? Terminology note, when you say as a "object, int or float" well, *everything* is an object, including strings. – juanpa.arrivillaga Apr 04 '23 at 17:39
  • @Barmar wellll there is no end to the hacky, brittle things you could do with live introspection of the call stack – juanpa.arrivillaga Apr 04 '23 at 17:41
  • IMHO what you *could* do that is **close** to what you want to do is: 1) **always** pass a string as argument 2) pass a separate `dict` argument called `env` 3) use `exec` to evaluate the expression inside `env`. In this way you have the string of the expression and the value it evaluates to. In other languages what you want may be possible using some macro/preprocessor trick maybe... – Bakuriu Apr 04 '23 at 17:43
  • @Bakuriu, suggesting `exec()` or `eval()` to someone who is clearly not in a position to asses the risk of running arbitrary code isn't wise – buran Apr 04 '23 at 17:45
  • [Why is using 'eval' a bad practice?](https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice) (Note that `ast.literal_eval` is not going to complain for `2+2`). – Ignatius Reilly Apr 04 '23 at 17:50
  • I tried with locals() but it keeps only 'any_given_argument' as the variable and the value given as argument. I thought there was a way of capturing the writen argument and its value as shown in the examples above. – Sandro S Apr 04 '23 at 17:53
  • Yes, there is a couple of ways, but they should be avoided because they are unsafe. As @buran noticed in comments, you shouldn't care about variable names, and printing the result of expressions like 2+2 can be done with `print`. So, if you tell us what are you going to use this function for, we may be able to propose another way of doing it. – Ignatius Reilly Apr 04 '23 at 17:58
  • Thanks for the advice!! I will use to show that a pandas on spark dataframe series is unique. psdf['col1'].is_unique will return true or false and I will use the print argument function to make it readable for a for loop with other columns to detecte whether a column has repeated elements. And this curiosity came to me about being able to print an argument input – Sandro S Apr 04 '23 at 20:33

0 Answers0