2

I would like something doing the following:

s = "abc"

print (func(s))

Ouput:

s

I've seen it seems not possible to do so, but still I try if someone knows

3
  • 4
    Why do you want to do this? Commented Jan 19, 2021 at 6:01
  • 4
    Possibly relevant: xyproblem.info Commented Jan 19, 2021 at 6:01
  • Essentially to read the code behind and to see how that could be possible. Commented Jan 19, 2021 at 6:15

2 Answers 2

4
from varname import nameof

s = 'abc'

print (nameof(s))

Output:

s

Package here:

https://github.com/pwwang/python-varname

Well, it is feasible, which does not mean it should be done. There is no difference with a print ("s") practically, plus there is dict if you want to associate a key and a value.

Sign up to request clarification or add additional context in comments.

4 Comments

I fail to see how nameof(s) is any better than "s"...
Yes right sorry. Well it is certainly useless, still it is funny.
You should actually use argname() in such a case. See my answer to this question. BTW, I am the author of the python-varname package.
@PanwenWang what a provocative work you did!
1
from varname import argname

def func(s):
  # if varname version < 0.7.2 
  # name = argname(s)
  name = argname('s')
  print(name, '=', s)

a = 1
b = 2

func(a) # a = 1
func(b) # b = 2

Disclaimer: I am the author of the python-varname package.

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.