String Variables to Variable Name?

Sebastian Southwell

I was just wondering if there was a way I could create variables from string like in the code below, I can create a file which includes the variable subject in the filename.

    filehandle = open('data_' + Subject + '.py', 'w')

And here I can create a new variable with the string name in the name when the file is imported.

    filehandle.write('High' + Game + 'Score = 0')

Which when I import it as a module, the variable name would be like HighMinecraftScore if Game = "Minecraft", HighMarioScore if Game = "Mario" etc.

I was just wondering if anyone knew how I accomplish the same results, as I'd hope to use it in a way in which I can create new variables with matching names autonomously.

Is there any way I can accomplish this without having to write or import modules?

Thank You

Jonathan

You can basically accomplish this with a dictionary. If you read the contents of the file into a dictionary with the key being the variable name and the score being the value. You've got your dynamic variables.

This is pretty much how python works behind the scenes. It uses a dictionary to store all variables and their values.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Getting PHP variable name as string for many variables

Using a variables string value in variable name

Set variable with a loop using the variables name as a string

How to get value of variables in Scala, where name of variable is obtained as string

Replace array-mapped variables with the actual variable name/string?

How to get the variable name as string from an array of variables?

Search for variable name in Pycharm without getting variables containing the same string

Environment variables and variable name

Use variables in the name of the variable

Make the string into the variables name

Call variable using another variable whos string content is the other variables' name

Batch string variables with variables in the string name

Python - Variable inside a variables name?

Getting the name of a variable as a string

Getting the name of a variable as a string

Call string with a variable name

Variable name as a string in Javascript

Convert Variable Name to String?

Using a string as a variable name

Get the name of a variable in a string

Using a string as variable name

Compare variable name to a string

String to variable name Haskell

Name a variable as string in Matlab

Variable string inside variable name

Using a string with variables as a function name

Changing string to an existing variable name to create 1 function for multiple variables (Inside class, using root from Tkinter)

Modifying a Variables Value from a string with the variables name

convert string value to variable name | string value contain variable name