Python assignment loop

Silver Rock silverfrequent at gmail.com
Mon May 21 00:12:05 EDT 2007


On 20 May 2007 20:21:52 -0700, George Sakkis <george.sakkis at gmail.com> wrote:
> On May 20, 10:33 pm, "Silver Rock" <silverfrequ... at gmail.com> wrote:
> > 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 :-)



More information about the Python-list mailing list