[Zope] evaluating a string as an expression

Lennart Regebro lennart@regebro.nu
Mon, 4 Mar 2002 10:03:00 +0100


From: "Greg Conway" <greg@gmlnt.com>
> I'm managing (very slowly!) to ease myself from DTML to Python, but I have
> just hit a bit of a snag...

Now, are you talking python scripts or python products? There are
differencies...

> What I've done is stored these values within a form using a loop

Store where?

> and now I
> need to retreive the values in python. I can see them in the request, and
> can access them via REQUEST.specify1, REQUEST.specify2, etc - but not
> programmatically!

That *is* programatically.

> So my question is (please somebody take pity on me!!), how can I perform
the
> following (pseudo code!!) in python?
>
> TestVar = ( 'REQUEST.specify' + str(OutsideLoop.CatID) )
>
> This doesn't work, obviously!

Why do you have single quotes around REQUEST.specify? That turns it into a
string, you know...

> What I really want to do is (I think)...
>
> TestVar = eval("REQUEST.specify" + str(OutsideLoop.CatID))

No it isn't, what you want to do it to get rid of the quotes. And you may
want to try REQUEST['specify'] instead. I'm not sure what type REQUEST is in
pythonscripts but in normal python it is a dictionary.

You might also want to look at using parameters instead of chugging
everything into REQUEST.