By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,772 Members | 906 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,772 IT Pros & Developers. It's quick & easy.

Python assignment loop

P: n/a
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.
May 21 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On May 20, 10:33 pm, "Silver Rock" <silverfrequ...@gmail.comwrote:
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

May 21 '07 #2

P: n/a
On 20 May 2007 20:21:52 -0700, George Sakkis <ge***********@gmail.comwrote:
On May 20, 10:33 pm, "Silver Rock" <silverfrequ...@gmail.comwrote:
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

--
http://mail.python.org/mailman/listinfo/python-list
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 :-)
May 21 '07 #3

P: n/a
On May 21, 8:12 am, "Silver Rock" <silverfrequ...@gmail.comwrote:
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.

May 21 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.