Python assignment loop

S

Silver Rock

i need to do something like this:

###
import wx
x=number
for i in range(500):
"var"+str(i)=ClassXYZ(...,x+i,...)

#.... code
y=number
for i in range(y):
ClassAAAA(object_called_by_the_string("var"+str(i)),...)

###
i can't figure out how to do this, and could not find it on the web.
c.
 
G

George Sakkis

i need to do something like this:

###
import wx
x=number
for i in range(500):
"var"+str(i)=ClassXYZ(...,x+i,...)

#.... code
y=number
for i in range(y):
ClassAAAA(object_called_by_the_string("var"+str(i)),...)

###
i can't figure out how to do this, and could not find it on the web.
c.

Whenever you are tempted to create dynamically variables names, 99% of
the time what you really want is a data structure, typically a dict or
a list. In your example, a list will do:

x=number
xyz_objects = [ClassXYZ(...,x+i,...) for i in xrange(500)]
#.... code
y=number
aaaa_objects = [ClassAAAA(object_called_by_the_string(xyz,...)
for xyz in xyz_objects[:y]]

If you can't figure out what this does, lookup for "list
comprehensions". By the way, I hope these were shortened examples and
you're not actually using names such as 'ClassAAAA' or 'ClassXYZ' in
your actual code...

George
 
S

Silver Rock

i need to do something like this:

###
import wx
x=number
for i in range(500):
"var"+str(i)=ClassXYZ(...,x+i,...)

#.... code
y=number
for i in range(y):
ClassAAAA(object_called_by_the_string("var"+str(i)),...)

###
i can't figure out how to do this, and could not find it on the web.
c.

Whenever you are tempted to create dynamically variables names, 99% of
the time what you really want is a data structure, typically a dict or
a list. In your example, a list will do:

x=number
xyz_objects = [ClassXYZ(...,x+i,...) for i in xrange(500)]
#.... code
y=number
aaaa_objects = [ClassAAAA(object_called_by_the_string(xyz,...)
for xyz in xyz_objects[:y]]

If you can't figure out what this does, lookup for "list
comprehensions". By the way, I hope these were shortened examples and
you're not actually using names such as 'ClassAAAA' or 'ClassXYZ' in
your actual code...

George

hi George,

thanks for your help.

yes, that is the way I a solving the problem. using lists. so it seems
that there is no way around it then..

cheers, i am not using ClassAAAA or ClassXYZ in my code :)
 
A

Alexey Borzenkov

yes, that is the way I a solving the problem. using lists. so it seems
that there is no way around it then..

There's at least one way to do it that I can think of straight away:

selfmodule = __import__(__name__, None, None, (None,))
setattr(selfmodule, "varname", value)

But I can't say it's anywhere near elegant.
 

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,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top