python function to create a variable

perkeyone

something clever
Reaction score
71
is it possible to make a function which will create a variable with a specified or unspecified name?

for example, lets say i want a function which when called will make a new variable and name it with an underscore followed by the date and time it was made. "_jan5,2010@00:43"

then i could set the value of _jan5,2010@00:43 to anything i want

another example, lets say i want to make a function which takes a string as a parameter, then creates a variable by that name.

such that i could do something like this
newVar("dog")
dog=0
 

Xienoph

You can change this now in User CP.
Reaction score
43
I'm not very familiar with python, but look around for "meta programming" and "reflection."
 

perkeyone

something clever
Reaction score
71
thanks xienoph
that definitely seems to be what im looking for,
although now that ive googled the subject im a little more confused,
this seems like it will be a lot harder than i thought,
 
Reaction score
333
"globals()["newVar"] = newValue" will create a new variable in the global namespace. What is the point of all this?
 

Siretu

Starcraft 2 Editor Moderator
Reaction score
293
It is not that hard.
Code:
def NewVar(name="Var"):
    V = name + date
    exec(V + " = 5")

The variable "date" would be the date. Look through the time module to find the function that gives the format that you want.

You can call the function with or without an argument. If you don't provide an argument, the variable will be called "Var" + date.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top