AW: Ternery operator

Discussion in 'Python' started by Uwe Schmitt, Sep 9, 2003.

  1. Uwe Schmitt

    Uwe Schmitt Guest

    :: Uwe Schmitt writes:
    :: > Normaly[sic] you should simulate "C ? T : F"
    :: >
    :: > either by
    :: > [T,F][not C]
    :: >
    :: > or
    :: >
    :: > (C and [T] or [F])[0]
    :: >
    :: > in the first case T and F are evaluated[sic] allways[sic],
    :: > the latter solution does short circuit evaluation,
    :: > which is according to the C/C++ semantics of the
    :: > ternary operator.
    ::
    :: NOT TRUE!
    ::
    :: NEITHER of your options does short-circuit evaluation. That's
    :: part of why many people (myself included) wanted to have a
    :: conditional expression (more accurate term than "ternary operator").

    The second one does short circuit evaluation, try the following:


    def T():
    print "T called"
    return "T"
    def F():
    print "F called"
    return "F"

    res1 = (1 and [T()] or [F()])[0]
    res2 = (0 and [T()] or [F()])[0]

    Greetings, Uwe.
     
    Uwe Schmitt, Sep 9, 2003
    #1
    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. Jakob Bieling

    Q: operator void* or operator bool?

    Jakob Bieling, Mar 5, 2004, in forum: C++
    Replies:
    2
    Views:
    586
    Rob Williscroft
    Mar 5, 2004
  2. John Smith
    Replies:
    2
    Views:
    423
    Ivan Vecerina
    Oct 6, 2004
  3. Alex Vinokur
    Replies:
    4
    Views:
    3,051
    Peter Koch Larsen
    Nov 26, 2004
  4. Andrew Chalk

    Ternery operator

    Andrew Chalk, Sep 7, 2003, in forum: Python
    Replies:
    15
    Views:
    1,071
    Peter Hansen
    Sep 9, 2003
  5. Michael Chermside

    RE: Ternery operator

    Michael Chermside, Sep 9, 2003, in forum: Python
    Replies:
    2
    Views:
    424
    Erik Max Francis
    Sep 9, 2003
Loading...

Share This Page