Pass a list of variables to a procedure

K

KRB

Hi there,

I would like to be able to pass a list of variables to a procedure, and have the output assigned to them.

For instance:

x=0
y=0
z=0

vars =[x,y,z]
parameters=[1,2,3]

for i in range(1,len(vars)):
*** somefunction that takes the parameter "1", does a computation and assigns the output to "x", and so on and so forth.

Such that later in the program I can
print x,y,z

I hope that makes sense, otherwise I have to do:
x=somefunction(1)
y=somefunction(2)
z=somefunction(3)
etc etc

Appreciate any help
 
N

Nobody

I would like to be able to pass a list of variables to a procedure, and
have the output assigned to them.

Use a dictionary or an object.

If the variables are globals (i.e. attributes of the current module), you
can pass the result of globals() into the function, or have the function
return a dictionary which the caller merges into globals().

There isn't no way to do anything similar for local variables. The parser
has to "see" the name being used a local variable in order for it to
actually exist as a local variable.
otherwise I have to do:
x=somefunction(1)
y=somefunction(2)
z=somefunction(3)

Or you could do:

x, y, z = [somefunction(i) for i in [1,2,3]]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top