Re: if the else short form

Discussion in 'Python' started by Philip Semanchuk, Sep 29, 2010.

  1. On Sep 29, 2010, at 7:19 AM, Tom Potts wrote:

    > This is just a sneaky shorthand, which is fine if that's what you want, but
    > it makes it harder to read. The reason it works is that 'fill==True' is a
    > boolean expression, which evaluates to True or False, but if you force a
    > True into being an integer, it will be 1, and a False will become 0. Try
    > writing 'True == 1' on the Python interpreter to see what I mean. So this
    > code snippet is creating a tuple with two elements, and then selecting the
    > first if 'fill==True' is False, or 0, and selecting the second if
    > 'fill==True' is True, or 1.
    >
    > As I say, this kind of coding is absolutely fine, but it makes things harder
    > to read and doesn't really save much space. I wouldn't recommend using this
    > kind of style yourself, at least until you're more familiar with programming
    > in Python.


    Does Python make any guarantee that int(True) == 1 and int(False) == 0 will always hold, or are their values an implementation detail?

    Thanks
    Philip


    > On 29 September 2010 11:42, Tracubik <> wrote:
    >
    >> Hi all,
    >> I'm studying PyGTK tutorial and i've found this strange form:
    >>
    >> button = gtk.Button(("False,", "True,")[fill==True])
    >>
    >> the label of button is True if fill==True, is False otherwise.
    >>
    >> i have googled for this form but i haven't found nothing, so can any of
    >> you pass me any reference/link to this particular if/then/else form?
    >>
    >> thanks
    >> Nico
    >> --
    >> http://mail.python.org/mailman/listinfo/python-list
    >>

    > --
    > http://mail.python.org/mailman/listinfo/python-list
    Philip Semanchuk, Sep 29, 2010
    #1
    1. Advertising

  2. On Wed, 29 Sep 2010 08:53:17 -0400 Philip Semanchuk
    <> wrote:

    > Does Python make any guarantee that int(True) == 1 and int(False) ==
    > 0 will always hold, or are their values an implementation detail?
    >


    <http://docs.python.org/release/3.1/reference/datamodel.html#the-standard-type-hierarchy>

    Booleans (bool)

    These represent the truth values False and True. The two objects
    representing the values False and True are the only Boolean
    objects. The Boolean type is a subtype of the integer type, and
    Boolean values behave like the values 0 and 1, respectively, in
    almost all contexts, the exception being that when converted to a
    string, the strings "False" or "True" are returned, respectively.


    /W


    --
    INVALID? DE!
    Andreas Waldenburger, Sep 29, 2010
    #2
    1. Advertising

  3. Andreas Waldenburger <> wrote:
    ><http://docs.python.org/release/3.1/reference/datamodel.html#the-standard-type-hierarchy>
    > [ ... ]
    > Boolean values behave like the values 0 and 1, respectively, in
    > almost all contexts, the exception being that when converted to a
    > string, the strings "False" or "True" are returned, respectively.


    Hmm. So the original problem of:

    button = gtk.Button(("False,", "True,")[fill==True])

    could also rewritten as:

    button = gtk.Button(str(bool(fill))+",")

    --
    \S

    under construction
    Sion Arrowsmith, Sep 30, 2010
    #3
  4. On 9/30/2010 3:21 AM Sion Arrowsmith said...
    > Andreas Waldenburger<> wrote:
    >> <http://docs.python.org/release/3.1/reference/datamodel.html#the-standard-type-hierarchy>
    >> [ ... ]
    >> Boolean values behave like the values 0 and 1, respectively, in
    >> almost all contexts, the exception being that when converted to a
    >> string, the strings "False" or "True" are returned, respectively.

    >
    > Hmm. So the original problem of:
    >
    > button = gtk.Button(("False,", "True,")[fill==True])
    >
    > could also rewritten as:
    >
    > button = gtk.Button(str(bool(fill))+",")
    >


    I think I'd prefer

    button = gtk.Button("%s," % bool(fill))

    Emile
    Emile van Sebille, Sep 30, 2010
    #4
  5. In message <>, Philip
    Semanchuk wrote:

    > Does Python make any guarantee that int(True) == 1 and int(False) == 0
    > will always hold, or are their values an implementation detail?


    There has never been a rationally-designed language where this was merely
    “an implementation detailâ€.
    Lawrence D'Oliveiro, Oct 5, 2010
    #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. David Geering

    longs, long longs, short short long ints . . . huh?!

    David Geering, Jan 8, 2007, in forum: C Programming
    Replies:
    15
    Views:
    546
    Keith Thompson
    Jan 11, 2007
  2. Replies:
    4
    Views:
    803
    Kaz Kylheku
    Oct 17, 2006
  3. Ioannis Vranos

    unsigned short, short literals

    Ioannis Vranos, Mar 4, 2008, in forum: C Programming
    Replies:
    5
    Views:
    660
    Eric Sosman
    Mar 5, 2008
  4. Hrvoje Niksic

    Re: if the else short form

    Hrvoje Niksic, Sep 29, 2010, in forum: Python
    Replies:
    15
    Views:
    483
    Hrvoje Niksic
    Oct 10, 2010
  5. Seebs

    Re: if the else short form

    Seebs, Sep 29, 2010, in forum: Python
    Replies:
    35
    Views:
    1,031
    John Nagle
    Oct 13, 2010
Loading...

Share This Page