0

Suppose I have two variables:

_first = True
_second = _first

print (getValue(_second))

>>> _first

Is there a way to implement getValue(or anything else) in that way ? I don't care about the real value (boole\int\string etc..) , just the name of the assigned variable.

Thanks

3 Answers 3

1

Variables don't refer to each other. When you use _second = _first, you aren't making _second refer to _first, you're making it refer to the value that _first refers to. This might clear some things up: Facts and Myths about Names and Values in Python.

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

1 Comment

Nice link, Ned! And it's nice to see you having fun with good old Graphviz. :) (FWIW, I programmed in various "traditional" languages (including C) for many years before coming to Python, but I'd learned about the binding approach to variables via PostScript, so I adapted to Python fairly quickly).
0

No; once you've assigned _first to _second, there is no longer any connection between _second and where it got its value from (as opposed to the value itself).

Comments

0

you cannot print out the variable's name in such a way.

But you might try using Dictionary so that you can have a key-value pair for each entry.

That way you can find the values corresponding to certain names as keys and get the key names as well. I hope that helps you.

to get help on dictionaries you can refer:

http://www.tutorialspoint.com/python/python_dictionary.htm

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.