Returning a string from a boolean

Discussion in 'Python' started by Dan Rawson, Aug 12, 2003.

  1. Dan Rawson

    Dan Rawson Guest

    I need to return "True" or "False" strings for a boolean value (mostly for display purposes).

    It's obviously trivial to write the function:

    def bStr (bVar):
    if bVar:
    return 'True'
    else:
    return 'False'

    In Perl I can do this with the ternary 'if'

    (bVar) ? 'True' : 'False'


    Is there a simpler way in Python??

    If it makes a difference, I'm using 2.2.2 (on Solaris) with no chance of going to 2.3 in the near future <g>; I know
    that some of this has changed in 2.3.

    TIA . . .

    Dan
     
    Dan Rawson, Aug 12, 2003
    #1
    1. Advertising

  2. Dan Rawson

    Duncan Booth Guest

    Dan Rawson <daniel.rawson.take!this!out!@asml.nl> wrote in
    news:bhalml$1012pe$-berlin.de:

    > In Perl I can do this with the ternary 'if'
    >
    > (bVar) ? 'True' : 'False'
    >
    >
    > Is there a simpler way in Python??
    >
    > If it makes a difference, I'm using 2.2.2 (on Solaris) with no chance
    > of going to 2.3 in the near future <g>; I know that some of this has
    > changed in 2.3.
    >

    Python 2.2 and earlier, the shortest way is:

    return bVar and 'True' or 'False'
    or
    return ('True','False')[not bVar]

    Both of the above will test the truth value of bVar, so for example an
    empty string or empty list will return False. Personally, I would go for
    your original function as combining clarity with reasonable but not
    excessive brevity.

    In Python 2.3, str(bVar) will give you 'True' or 'False' as appropriate,
    but only if bVar is a bool.

    --
    Duncan Booth
    int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
    "\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
     
    Duncan Booth, Aug 12, 2003
    #2
    1. Advertising

  3. Dan Rawson

    Dan Rawson Guest

    Duncan Booth wrote:
    > Dan Rawson <daniel.rawson.take!this!out!@asml.nl> wrote in
    > news:bhalml$1012pe$-berlin.de:
    >
    >
    >>In Perl I can do this with the ternary 'if'
    >>
    >>(bVar) ? 'True' : 'False'
    >>
    >>
    >>Is there a simpler way in Python??
    >>
    >>If it makes a difference, I'm using 2.2.2 (on Solaris) with no chance
    >>of going to 2.3 in the near future <g>; I know that some of this has
    >>changed in 2.3.
    >>

    >
    > Python 2.2 and earlier, the shortest way is:
    >
    > return bVar and 'True' or 'False'
    > or
    > return ('True','False')[not bVar]
    >
    > Both of the above will test the truth value of bVar, so for example an
    > empty string or empty list will return False. Personally, I would go for
    > your original function as combining clarity with reasonable but not
    > excessive brevity.
    >
    > In Python 2.3, str(bVar) will give you 'True' or 'False' as appropriate,
    > but only if bVar is a bool.
    >


    OK, thanks. I tend to use the one-line version because I can stick it inside (for example) a 'print' statement.
     
    Dan Rawson, Aug 12, 2003
    #3
  4. On Tue, 12 Aug 2003 12:26:07 +0000 (UTC), Duncan Booth <> wrote:

    >Dan Rawson <daniel.rawson.take!this!out!@asml.nl> wrote in
    >news:bhalml$1012pe$-berlin.de:
    >
    >> In Perl I can do this with the ternary 'if'
    >>
    >> (bVar) ? 'True' : 'False'
    >>
    >>
    >> Is there a simpler way in Python??
    >>
    >> If it makes a difference, I'm using 2.2.2 (on Solaris) with no chance
    >> of going to 2.3 in the near future <g>; I know that some of this has
    >> changed in 2.3.
    >>

    >Python 2.2 and earlier, the shortest way is:
    >
    > return bVar and 'True' or 'False'
    >or
    > return ('True','False')[not bVar]
    >
    >Both of the above will test the truth value of bVar, so for example an
    >empty string or empty list will return False. Personally, I would go for
    >your original function as combining clarity with reasonable but not
    >excessive brevity.
    >
    >In Python 2.3, str(bVar) will give you 'True' or 'False' as appropriate,
    >but only if bVar is a bool.


    which you can ensure by str(bool(anyvar)) ;-)

    Regards,
    Bengt Richter
     
    Bengt Richter, Aug 12, 2003
    #4
    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. Replies:
    11
    Views:
    699
    Christos Georgiou
    May 2, 2006
  2. J Leonard
    Replies:
    4
    Views:
    12,891
    Mark Space
    Jan 19, 2008
  3. Replies:
    1
    Views:
    713
    Cowboy \(Gregory A. Beamer\)
    Nov 28, 2007
  4. Replies:
    0
    Views:
    84
  5. Metre Meter
    Replies:
    7
    Views:
    464
    Metre Meter
    Aug 6, 2010
Loading...

Share This Page