Function - How to create variables from arguments in python

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I want to make a function in python were one of the arguments is a variable later...

as an example it should be like this

foo=myfunction("var1","var2",otherarguments)

(note that var1 and var2 are string never used before)

then the program should make

var1=blabla
var2=blabla2/otheraguments

i need this in order make graphs using GNUplot were a variable will be used to set the parameters of the graph

for example this should be inside the function

var1=Gnuplot.Gnuplot(debug=1)
var1("set terminal gif animate delay 10") #to make animation in gnuplot
var1("set style data lines")
var1.xlabel("name_of_x_axis")
var1("set pm3d")
var1.title("newgraph in 3d")
var1.splot(Gnuplot.GridData(otherarguments,X,Y,binary=0))

i tried with something like this

example={"var1":"Gnuplot.Gnuplot(debug=1)","var2":[1,2,3,4,5,6,7,8,9]}

and then

locals().update(example)

or

globals().update(example)

but i m not sure how to implement this in a function

Answers

to create a variable from string use vars()

>>> foo="bar"
>>> vars()[foo] =  qwertry 
>>> print bar  # -->  qwertry 

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/10623751/how-to-create-variables-from-arguments-in-python

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils