You can change the name of an object in Python by assigning it to a new variable. You can do this based on a condition using conditional statements.
For example, if you want to change the name of the variable a to c if a condition is met, you can do the following:
main.py32 chars4 lines
In this code, if the condition is True, a will be assigned to a new variable c. You can then use c in place of a throughout the rest of your code.
Alternatively, you can rename the variable a to c using string manipulation:
main.py45 chars4 lines
In this code, globals() is a built-in function in Python that returns a dictionary of the current global symbol table. Here, we are using globals()['c'] to create a new variable c and assign it the value of a. Note that this approach modifies the global namespace, which may not be desirable in all cases.
gistlibby LogSnag