For review: PEP 308 - If-then-else expression

James J. Besemer jb at cascade-sys.com
Sun Feb 9 04:32:08 EST 2003


Some data:

A quick perusal of Python interpreter source code reveals several hundred 
uses of the ?: operator.

Clearly the estimed BDFL evidently finds C's ?: as is to be fairly useful, at 
least sufficiently useful to employ it routinely.

Here are some examples:

Include/abstract.h:	
	(PyList_Check(o) ? PyList_GET_SIZE(o) : PyTuple_GET_SIZE(o))
	(PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i))

Modules/_hotshot.c:
	result = (self->logfp == NULL) ? Py_True : Py_False;
	(self->frametimings ? "yes" : "no"));
	(self->lineevents ? "yes" : "no"));

Modules/_sre.c:
	#define SRE_LOC_IS_DIGIT(ch) ((ch) < 256 ? isdigit((ch)) : 0)
	#define SRE_LOC_IS_SPACE(ch) ((ch) < 256 ? isspace((ch)) : 0)
	#define SRE_LOC_IS_ALNUM(ch) ((ch) < 256 ? isalnum((ch)) : 0)

Modules/binascii.c:		
	c = (c>9) ? c+'a'-10 : c + '0';

Modules/cPickle.c:
	size = ((n < 32) ? 32 : n);

[many, many more where these came from.]

If it's good enough for Python, the implementation -- then it's good enough 
for Python, the language.

--jb

-- 
James J. Besemer		503-280-0838 voice
2727 NE Skidmore St.		503-280-0375 fax
Portland, Oregon 97211-6557	mailto:jb at cascade-sys.com
				http://cascade-sys.com	







More information about the Python-list mailing list