How to convert a ">" into a >

Discussion in 'Python' started by dominique, Jun 21, 2008.

  1. dominique

    dominique Guest

    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
     
    dominique, Jun 21, 2008
    #1
    1. Advertising

  2. dominique

    John Machin Guest

    On Jun 21, 9:17 pm, dominique <> 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
     
    John Machin, Jun 21, 2008
    #2
    1. Advertising

  3. dominique

    dominique Guest

    On Jun 21, 1:37 pm, John Machin <> wrote:
    >
    > Look at the operator module. In your above example:
    >
    > return {
    > '>': operator.gt,
    > '=': operator.eq,
    > '<': operator.lt,
    > }[variable]
    >
    > Cheers,
    > John


    Thanks a lot John
    Dominique
     
    dominique, Jun 21, 2008
    #3
  4. dominique

    Tim Roberts Guest

    dominique <> wrote:

    >On Jun 21, 1:37 pm, John Machin <> wrote:
    >>
    >> 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 ):
    --
    Tim Roberts,
    Providenza & Boekelheide, Inc.
     
    Tim Roberts, Jun 22, 2008
    #4
  5. dominique

    dominique Guest

    On Jun 22, 6:45 am, Tim Roberts <> wrote:
    > dominique <> wrote:
    > >On Jun 21, 1:37 pm, John Machin <> wrote:

    >
    > >> 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 ):
    > --
    > Tim Roberts,
    > Providenza & Boekelheide, Inc.


    Thanks for the tip.
    Dominique
     
    dominique, Jun 22, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    1
    Views:
    412
    Karl Seguin
    Jul 23, 2003
  2. Harry Zoroc
    Replies:
    1
    Views:
    978
    Gregory Vaughan
    Jul 12, 2004
  3. Jaap
    Replies:
    4
    Views:
    730
    John O'Conner
    Jul 10, 2006
  4. Klaus Neuner
    Replies:
    7
    Views:
    515
    Klaus Neuner
    Jul 26, 2004
  5. sck10
    Replies:
    4
    Views:
    16,054
    daniel
    Sep 3, 2006
Loading...

Share This Page