Adding a conditional expression in Py3.0
Sorry for looking in every hole. Just a suggestion. A= condition and first or second problem is in case when first in (None,0,[],""). May be invent new operator 'take'. take - returns right operator when left evals to True and stops computing condidtional expression. Then we could write: A = condition take first or second. A = x==y take w or s A = z is not None and q!=12 take [] or allowable(z,q) take [(z,q)] or "Impossible" Ok, it might looks ugly. But may be not. ------------------------------------- Excuse my english.
Sokolov Yura wrote:
Sorry for looking in every hole. Just a suggestion.
A= condition and first or second problem is in case when first in (None,0,[],""). May be invent new operator 'take'. take - returns right operator when left evals to True and stops computing condidtional expression. Then we could write:
A = condition take first or second. A = x==y take w or s A = z is not None and q!=12 take [] or allowable(z,q) take [(z,q)] or "Impossible"
Ok, it might looks ugly. But may be not.
One of the advantages of (if x then y else z) is that it doesn't require the introduction of a new keyword (I think the "then" could be special- cased like "as" in the import statement). Reinhold -- Mail address is perfectly valid!
On Sun, 2005-09-25 at 19:11 +0200, Reinhold Birkenfeld wrote:
Sokolov Yura wrote:
Sorry for looking in every hole. Just a suggestion.
A= condition and first or second problem is in case when first in (None,0,[],""). May be invent new operator 'take'. take - returns right operator when left evals to True and stops computing condidtional expression. Then we could write:
A = condition take first or second. A = x==y take w or s A = z is not None and q!=12 take [] or allowable(z,q) take [(z,q)] or "Impossible"
Ok, it might looks ugly. But may be not.
One of the advantages of (if x then y else z) is that it doesn't require the introduction of a new keyword (I think the "then" could be special- cased like "as" in the import statement).
This wouldn't look so bad either: (if x: y else: z) 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.
Reinhold
-- Gustavo J. A. M. Carneiro <gjc@inescporto.pt> <gustavo@users.sourceforge.net> The universe is always one step beyond logic.
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. Regards, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia --------------------------------------------------------------- http://boredomandlaziness.blogspot.com
participants (4)
-
Gustavo J. A. M. Carneiro -
Nick Coghlan -
Reinhold Birkenfeld -
Sokolov Yura