[python] creating variable names by adding 2 strings?
Hi there, I'm creating variables in python 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, Lee (crossing his fingers)
Hi Lee, You could use a dictionary for this: vars = {} while (p!=0): vars['p'+`p`] = string.replace(component[control], ",", "") p=p-1 control=control+1 then 'vars' will contain keys (e.g., 'p1', 'p2' etc.. ) and corresponding values. If that's totally not what you want.. a little more context would help. ;-) -steve ---------------------------------------------------------------------- Hi there, I'm creating variables in python 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, Lee (crossing his fingers) _______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Thanks, Steve. This'll do nicely ;-) BUT... For some strange reason the following doesn't work in my Python *method*: dict = {} dict["Lee"] = 1 Error: Error Type: Python Method Error Error Value: Forbidden operation STORE_SUBSCR at line 3! <<-- ????? Which seems very strange. No matter what I try and store I get the same error. dic = {'two': 2, 'three': 3} return dic['two'] The above works fine but when I try and add to it I get the same error. If this is a simple mistake I'm making then I plead stupidity!!! Can someone tell me how to do this in my python method? I've tested all my code in IDLE and it works fine - why not in Zope? Answers on a postcard to the usual address! Thanks very much, guys. - Best regards, Lee Steve Spicklemire wrote:
Hi Lee,
You could use a dictionary for this:
vars = {}
while (p!=0): vars['p'+`p`] = string.replace(component[control], ",", "") p=p-1 control=control+1
then 'vars' will contain keys (e.g., 'p1', 'p2' etc.. ) and corresponding values.
If that's totally not what you want.. a little more context would help.
;-)
-steve
Okay, I figured it out. - Silly Lee
participants (2)
-
Lee -
Steve Spicklemire