[SOLVED] how do i rename a variable in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 17

[SOLVED] how do i rename a variable in python?

not transfer the value, rename.

30th Nov 2018, 3:57 AM
LONGTIE👔
LONGTIE👔 - avatar
38 Answers
+ 13
Jay Matthews what if we can hack into the symbol table or something like that? Until two days ago, I didn't believe it possible to change the "value" of a number. A 5 is a 5. Well, take a look: https://code.sololearn.com/cMqzU4qx7WYG/?ref=app
30th Nov 2018, 4:48 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 12
Kishalaya Saha dude you are on a whole 'nother level 😲 Jay Matthews your probably right about the renaming/reassigning thing i cant really think of a way around it.
30th Nov 2018, 5:26 AM
LONGTIE👔
LONGTIE👔 - avatar
+ 9
https://code.sololearn.com/c2f4kNE7DHdM/?ref=app I've added comments to it.. But is this really it?
30th Nov 2018, 6:42 PM
Yash✳️
Yash✳️ - avatar
+ 7
SlickBack your totally right about me over thinking a simple thing. although Kishalaya Saha and Yash✳️'s answers are quite interesting o a different level.
1st Dec 2018, 4:42 AM
LONGTIE👔
LONGTIE👔 - avatar
+ 6
why you want to rename a variable? you can just change it backspace and then type whatever you want ... you can rename a variable like a kid shown below... x=2 y=x print(y) same output when your print x or y
30th Nov 2018, 5:40 PM
Ratnapal Shende
Ratnapal Shende - avatar
+ 5
Kaveis let me try to give you an analogy. In a computer, we can rename a file, and also save a copy of it under a different name. In the second method, the original name still remains (though we can delete it afterwards). I think what LONGTIE is looking for is direct renaming. Reassigning only "transfers the value", like they said quite succinctly. It's an interesting question. I'm curious to know the answer too. May not be so easy; Python is good at hiding this kind of stuff.
30th Nov 2018, 4:19 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 5
Kishalaya Saha oh, ok. Thanks. Sorry i cant help LONGTIE👔✌ , but then this stuff is way over my head 😁
30th Nov 2018, 4:21 AM
Kav
Kav - avatar
+ 4
dont you just... rename it? Like, reassign it? Sorry, i dont know if i understand your question
30th Nov 2018, 4:12 AM
Kav
Kav - avatar
+ 4
TL;DR: I tried symtable and bytecode (opcode 90: STORE_NAME) to overwrite the name with/without setting the variable's value. Needs more testing/ideas (or, wrong area?) Yesterday I tried opcode 90* (and omitting the const loaded for it to store) because it takes a name index (so I thought: overwrite name stored there?): https://docs.python.org/3/library/dis.html#opcode-STORE_NAME "STORE_NAME(namei) : Implements name = TOS [top of stack]. namei is the index of name in the attribute co_names of the code object. The compiler tries to use STORE_FAST or STORE_GLOBAL if possible." ...but it's fiddly and I've only succeeded in annoying marshal.loads(...modified bytecode...). Related code: import dis, marshal, opcode #import symtable # needs more looking? print(opcode.opmap) exe=compile('AAA=67;BBB=69', '?', 'exec') dis.dis(exe) print(list(marshal.dumps(exe))) print(*dis.get_instructions(exe), sep='\n') Interesting: 90: STORE_NAME 91: DELETE_NAME 101: LOAD_NAME 108: IMPORT_NAME ...modding by hand.
1st Dec 2018, 2:39 PM
Kirk Schafer
Kirk Schafer - avatar
+ 3
Best way as far as I know : a = 5 b = a del a This is not directly changing the name. Variable b may be in a different place to variable a, as it does not copy where it is stored.
30th Nov 2018, 8:21 AM
Eragon1980
Eragon1980 - avatar
+ 3
Okay, I don't know about LONGTIE, but for me it is purely curiosity. I just want to understand Python better. I have no use for it as of now. And please read previous answers before posting your own. The assign_and_del method (which isn't exactly renaming) has been suggested quite a few times already.
30th Nov 2018, 6:18 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 3
Yash✳️ your solution is closest to what I was looking for. It's really clever! Thanks a lot for the experiments with locals(). I had totally forgotten about them! However, it does feel like you're transferring the value and deleting the name at the same time with pop(). And id() is not really a good test for this. In Python, whenever we do a = b, the id of a equals that of b. But this might be the best we can hope for. Thanks again! 😊
30th Nov 2018, 8:13 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 3
If Assigning and deleting , or editing cant help its hard to answer cause you can't rename variables directly , using dict or list can help u
30th Nov 2018, 8:58 PM
HaHu
HaHu - avatar
+ 3
Mohit the coder (M.S.D) spss can help to ,you have to install spss python plug in but directly i dont tink do
30th Nov 2018, 9:09 PM
HaHu
HaHu - avatar
+ 2
a = 6 print(a) b = a del a print(b)
30th Nov 2018, 7:45 AM
Linus Gates
Linus Gates - avatar
+ 2
renaming is essentially removing the old name and putting in a new name. in that sense reassigning the variable to a NEW variable name and deleting the old is essentially renaming. Outside of that use a text editor to find all cases of the original name and replace it with the new name. Kishalaya Saha I have to disagree that this method is not "exactly" renaming. It seems to be exactly what it is doing. It reassigned the existing variable to a new name hence renamed. ~Edit That said the catch is from the line you delete the old variable You have to make sure not to use that variable name again. It will only exist as a copy in the new variable. If you wanted to change it from the original declaration on then search and replace is the way to go. In this way Kishalaya is right. The del method may not be the most optimal way to achieve this. I guess this is why carefully choosing your names is as important as any other part of your design.
1st Dec 2018, 2:08 AM
James
James - avatar
+ 2
thanx Kishalaya Saha and SlickBack When we do a = 1 b = a Doesn't that mean b will point to the same memory address as that of a? Because when i use ctypes.addressof to check for their addresses, it's the same.. If, as they said on SO that when we use pop(), it deletes the previous var and then creates new one, then the address of the old and new var should be diff. right? But it's the same
1st Dec 2018, 9:16 AM
Yash✳️
Yash✳️ - avatar
+ 2
I think, Python doesn't know ahead of time what's on a line, which is which I can swap out the code of a function without changing the function's existence: https://code.sololearn.com/crDsM8YZAUS0/ Aside from swapping the pointer out from under Python (Kishalaya Saha -- neat; I was wondering if ctypes would work for self-modifying code too), what I think we're trying to do is access Python's name<->memory location mapping table, and change the lookup name. Though I've read "it doesn't work that way", I feel like this should work--the interpreter should just accept it--if a table (managing memory mappings, not the dictionaries in __builtins__) is available. In-language (i.e., no going behind the curtain) "marshal" felt like the best bet, but I'm afraid it might have to be a dirty hack, because there's the official way...not really working...combined with the reality that marshal.loads()--like PHP's object loader--is definitely not (intended to be) safe so...arbitrary overwrites exist.
1st Dec 2018, 4:25 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
здесь есть русские ?
1st Dec 2018, 8:21 PM
MAMON
MAMON - avatar
30th Nov 2018, 5:18 PM
MsJ
MsJ - avatar