+ 1
Two names for one variable
Is it possible to have two names for the same variable? I want to have a long name that describes what it is and a short name that I can use to make Codelines shorter. Here an Exemplar what I want to get: variableName, vn = 5 vn += 1 print(variableName) output: 6
5 Respuestas
+ 2
you could create two lists
variableName = [5]
vn = variableName
Now changing vn will also lead to a change in variableName and vice versa..
+ 1
you're thinking of tuples which, if I recall, must be assigned a tuple value.
+ 1
yeah, since python variables are just references to individual objects, trying this like in c++ won't work. assigning variableName to vn as named variable won't work since the "pointer" is copied, and doesn't reference the same same address.
I do think @Shatrunjay's example of lists will be easiest and probably most common approach to python names when wanting to emulate storage locations.
you could however create a custom class to emulate references:
#not my code, I can't take credit
class Reference:
def __init__(self, val):
self._value = val #refers to val without copying
def get(self):
return self._value
def set(self, val):
self._value = val
0
I don't think tuples are what I'm looking for.
Could you show me how to use tuples to solve the example that I wrote?
0
Thanks for all the answer. I think the best is I get used to long Variable names. Other solution might generate more potentially problems then they are helping.