How to convert a ">" into a >

D

dominique

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 ?

Thanks in advance

Dominique
 
J

John Machin

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
 
D

dominique

Look at the operator module. In your above example:

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

Cheers,
John

Thanks a lot John
Dominique
 
T

Tim Roberts

dominique said:
Look at the operator module. In your above example:

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

Thanks a lot John
Dominique

Yes, but you need to remember that what you are getting is not literally an
operator. That is, if you store that return value in a variable called
"op", you can't say this:

if x op y:
....

Instead, what you have is a function, so you'll have to write it:

if op( x, y ):
 
D

dominique

dominique said:
Look at the operator module. In your above example:
return {
'>': operator.gt,
'=': operator.eq,
'<': operator.lt,
}[variable]
Thanks a lot John
Dominique

Yes, but you need to remember that what you are getting is not literally an
operator. That is, if you store that return value in a variable called
"op", you can't say this:

if x op y:
....

Instead, what you have is a function, so you'll have to write it:

if op( x, y ):

Thanks for the tip.
Dominique
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top