473,322 Members | 1,232 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Returning a string from a boolean

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

Jul 18 '05 #1
3 19425
Dan Rawson <da**************************@asml.nl> wrote in
news:bh*************@ID-122008.news.uni-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 du****@rcp.co.uk
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?
Jul 18 '05 #2
Duncan Booth wrote:
Dan Rawson <da**************************@asml.nl> wrote in
news:bh*************@ID-122008.news.uni-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.

Jul 18 '05 #3
On Tue, 12 Aug 2003 12:26:07 +0000 (UTC), Duncan Booth <du****@NOSPAMrcp.co.uk> wrote:
Dan Rawson <da**************************@asml.nl> wrote in
news:bh*************@ID-122008.news.uni-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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Anand Natrajan | last post by:
Hi! I have a VB .NET client that is accessing an rpc/encoded service deployed in Axis 1.2. One of the operations, whoami, returns a string array. The server-side Java signature of this method is...
0
by: John Constant | last post by:
Using the MS C++ Example http://support.microsoft.com/default.aspx?scid=kb;en-us;261003 I've successfully managed to trap and log Script Errors that are generated by the WebBrowser (IE 6) which is...
1
by: Knecke | last post by:
Hi all. I have a problem with returning a custom Result object with webservice. The classes i use is described below (some fields and properties is removed) public class Result { int...
5
by: LS | last post by:
Can a WebMethod return an Interface type? Can we pass an interface parameter ? Example : public interface IEntity { long Id { get; set; } string Name { get; set; } }
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
1
by: J. Askey | last post by:
I am implementing a web service and thought it may be a good idea to return a more complex class (which I have called 'ServiceResponse') in order to wrap the original return value along with two...
0
by: TMesh | last post by:
Hello Is it possible to return a Crystal ReportDocument from a WebService? I keep getting the following error: Exception: System.Web.Services.Protocols.SoapException: Server was unable to...
4
by: scparker | last post by:
Hello, We have a stored procedure that does a basic insert of values. I am then able to retrieve the ID number created for this new record. We are currently using ASP.NET 2.0 and use N-Tier...
0
by: jaywak | last post by:
Just tried running some code on Linux (2.4.21-32.0.1.EL and Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)) and Windows XPSP2 (with Java HotSpot(TM) Client VM (build...
50
by: Mahendra | last post by:
I have following code snippet - int *createIncidenceMatrix(int numEdges, int numVertices) { int *p = (int *) calloc((numEdges*numVertices), sizeof(int)); if(p == NULL) { printf("Could not...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.