[Python-Dev] Adding a conditional expression in Py3.0

Nick Coghlan ncoghlan at gmail.com
Tue Sep 27 23:49:24 CEST 2005

Gustavo J. A. M. Carneiro wrote:
>   More realistic example:
>     def greet(person=None):
> 	print "Hello %s" % (if person is None: "World" else: person)
>   Not as compact as C's ?:, but more readable and intuitive.  It's just
> like an if-else construct, but on a single line and () around to make it
> look like an expression instead of a statement.

It looks even more like an expression without any embedded colons ;)

   def greet(person=None):
       # Infix conditional
       print "Hello %s" % ("World" if person is None else person)

   def greet(person=None):
       # Negated infix conditional so that 'normal' value is first
       print "Hello %s" % (person if person is not None else "World")

   def greet(person=None):
       # Prefix conditional
       print "Hello %s" % (if person is None then "World" else person)

Anyway, Guido's already given some indication that the PEP 308 infix and 
prefix conditional operators without colons (above) are the main options he is 
considering choosing from.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-Dev mailing list