0

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

3
  • 2
    Use Python dictionaries to do this docs.python.org/tutorial/datastructures.html#dictionaries Commented May 16, 2012 at 17:43
  • 4
    What are you actually trying to accomplish? Commented May 16, 2012 at 17:44
  • im sorry what i want to do later is use the last line var1.splot(Gnuplot.GridData(otherarguments,X,Y,binary=0)) with other arguments in order to make the gif... this shoud be outside the funcion (the function it should be only for the set up frame of gnuplot and the first image in a gif Commented May 16, 2012 at 18:00

2 Answers 2

1

to create a variable from string use vars()

>>> foo="bar"
>>> vars()[foo] = 'qwertry'
>>> print bar  # --> 'qwertry'
Sign up to request clarification or add additional context in comments.

1 Comment

From the docs: "Note The contents of this dictionary should not be modified; changes may not affect the values of local and free variables used by the interpreter." (vars() being equivalent to locals()) docs.python.org/library/functions.html#locals
1

Edit: demonstrate return a reference to a plot for later use

def myfunction():
    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")
    return var1

myplot = myfunction()
# Now call myplot.splot with whatever arguments you want.
myplot.splot(Gnuplot.GridData(otherarguments, X, Y, binary=0))
myplot.splot(Gnuplot.GridData(morearguments, X, Y, binary=0))
myplot.splot(Gnuplot.GridData(stillmore, X, Y, binary=0))
for args in list_of_arguments:
    myplot.splot( Gnuplot.GridData(args, X, Y, binary=0) )

2 Comments

i want to create multiple plots from the same function... so i need to my function enter the name of the variable where later y will plot so this variables shoudent be only inside this function... this is in fact an animation so the last line will be repited in each frame of the gif and what changes is the otheraguments
Then you should have your function return a reference to the plot you create.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.