Python Forum
Is it possible to create a variable with the name of an output of another variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is it possible to create a variable with the name of an output of another variable
#1
I'm making a console application, and i want to create a feature where you can create your own command if you feel like it's needed. Here is my code:
else: print('Unknown command entered. Would you like to give this command a meaning') temporary_console_input = console console = input('Y or N? ') creating_new_command_temp_waiting = True while creating_new_command_temp_waiting == True: if console == 'y': # Placeholder else if console == 'Y': # Placeholders else if console == 'n': # Placeholders else if console == 'N': # Placeholders else if console == 'sys.exit(0)': print('Shutting down') sys.exit(0) else: print('Unknown Option Entered. Please try again.') creating_new_command_temp = console logger.info('Creating command: "' + console + '"')
I would like to know if there is a way to create a variable with the name of console's current state. (e.g. if console is set to 4, create a variable named 4)
Self-taught HTML, CSS, Python, and Java programmer
Reply
#2
Valid Python name must start either with a letter or underscore(s) - it cannot start with a digit.

There are several possible methods to create a variable name dynamically - and none of them is recommended. One of them - by use of globals and locals functions
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
(Jun-04-2018, 11:19 PM)volcano63 Wrote: Valid Python name must start either with a letter or underscore(s) - it cannot start with a digit.

There are several possible methods to create a variable name dynamically - and none of them is recommended. One of them - by use of globals and locals functions

I know that, it was an example. Can you just tell me how i would create a variable with the name of another variable's value?
Self-taught HTML, CSS, Python, and Java programmer
Reply
#4
Output:
In [14]: a = 'c' In [15]: globals()[a] = 1 In [16]: c Out[16]: 1 In [17]: b = 'd' In [18]: exec('{} = 10'.format(b)) In [19]: b Out[19]: 'd' In [20]: d Out[20]: 10
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
(Jun-04-2018, 11:37 PM)volcano63 Wrote:
Output:
In [14]: a = 'c' In [15]: globals()[a] = 1 In [16]: c Out[16]: 1 In [17]: b = 'd' In [18]: exec('{} = 10'.format(b)) In [19]: b Out[19]: 'd' In [20]: d Out[20]: 10

Is this like 'put in line 16 instead of 1', or 'put in line 17'?
Self-taught HTML, CSS, Python, and Java programmer
Reply
#6
Line 15 creates variable with name c, line 18 - with name d
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#7
I solved it myself.
Self-taught HTML, CSS, Python, and Java programmer
Reply
#8
(Jun-06-2018, 06:14 PM)Panda Wrote: I solved it myself.
No thank you, I guess?! Another name to ignore list
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#9
(Jun-06-2018, 07:06 PM)volcano63 Wrote:
(Jun-06-2018, 06:14 PM)Panda Wrote: I solved it myself.
No thank you, I guess?! Another name to ignore list
Sorry. I didn't thank you because i'm ok at programming and have NO idea what it is you said i should do.
Self-taught HTML, CSS, Python, and Java programmer
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  link variable to exc blocks garbage collection Astrobert 1 673 Nov-19-2025, 05:03 AM
Last Post: Gribouillis
  variable changing types for error? Azdaghost 1 2,330 Sep-15-2025, 06:45 PM
Last Post: Gribouillis
  Functions: why use a variable "display" in a UDF as a Boolean MMartin71 3 1,078 May-26-2025, 05:55 AM
Last Post: DeaD_EyE
  PYTHONHOME Variable correct setting msetzerii 0 1,216 May-25-2025, 11:48 PM
Last Post: msetzerii
  how to get variable Azdaghost 3 1,099 Apr-23-2025, 07:43 PM
Last Post: deanhystad
  I trying to automate the Variable Logon button using the python code but I couldn't surendrasamudrala 0 714 Mar-07-2025, 05:02 AM
Last Post: surendrasamudrala
  not able to call the variable inside the if/elif function mareeswaran 3 1,497 Feb-09-2025, 04:27 PM
Last Post: mareeswaran
  creating arbitrary local variable names Skaperen 9 2,948 Sep-07-2024, 12:12 AM
Last Post: Skaperen
  Variable Substitution call keys Bobbee 15 4,551 Aug-28-2024, 01:52 PM
Last Post: Bobbee
  how solve: local variable referenced before assignment ? trix 5 2,553 Jun-15-2024, 07:15 PM
Last Post: trix

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.