How to convert a ">" into a >

John Machin sjmachin at lexicon.net
Sat Jun 21 13:37:30 CEST 2008


On Jun 21, 9:17 pm, dominique <MyDom... at gmail.com> wrote:
> Hello All,
>
> In a wx GUI,  I would like to let the user choose between >, < or =.
> So, I created a combobox and when the user chooses ">" for instance, I
> wanted to return > (the objective is to send the operator into another
> complex method):
> Example:
> if variable == ">":
> return >
>
> But this is invalid syntax.
>
> How can I transform a ">" into the > operator ie without parenthesis,
> so that I can load it into another function ?

Look at the operator module. In your above example:

return {
   '>': operator.gt,
   '=': operator.eq,
   '<': operator.lt,
   }[variable]

Cheers,
John



More information about the Python-list mailing list