creating variable names by adding 2 strings?

Greg Jorgensen gregj at pobox.com
Sun Jan 21 23:53:59 EST 2001


An array or list is the usual way to do what you describe, but if you
really want to create a bunch of new variable names, you can use exec:

  exec('p' + str(p) + '="' + \
      string.replace(component[control], ",", "") + '"')

exec() executes a string containing any Python code, in the namespace
of the calling code. To debug this you may want to create a string
first, then execute the string; that way you can print the string
during testing.

--
Greg Jorgensen
Portland, Oregon, USA
gregj at pobox.com


In article <3A6BAA32.370A2C00 at ntlworld.com>,
  lee.reilly at ntlworld.com wrote:
> Hi there,
>
> This is my first post here. I've got a problem, which I can find any
> solutions for using my books or docs on the WWW. I would really
> appreciate it if someone could tell me whether the following is
> possible...
>
> I'm creating variables in but I am having trouble creating them when
> they're *named* using other variables. Here's an example;
>
> while (p!=0):
>         p+`p`= string.replace(component[control], ",", "")
>         # e.g. I want 'p1 = string.replace.blah...'
>         p=p-1
>         control=control+1
>
> ==> SyntaxError: can't assign to operator :(
>
> I've tried various things but I cannot find a solution. I've got a
nasty
> feeling that it's not possible...
>
> If someone could confirm this or hopefully, tell me how to do it I
would
> be_extremely_grateful.
>
> Thanks.
>
> - Best regards,
>
> Lee
>
> --
> Lee Reilly
> mailto:lee.reilly at ntlworld.com
>
> ø¤º°`°º¤ø,,,,ø¤º°`°º¤ø,,,,ø¤º°`°º¤ø,,,,ø¤º°`°º¤ø,,,,
>
> HAVE SKILLS; WILL TRAVEL.
>
> I'm currently looking for an internship in the USA
> in the field of web development. Ideally, I'm looking
> for a placement for 3 to 18 months. Can you help?
>
> More details, my resume, etc. @
> http://homepages.strath.ac.uk/~aeu97172/
>
> </shameless plug>
>
>


Sent via Deja.com
http://www.deja.com/



More information about the Python-list mailing list