Conditional operator in Python?

Donn Cave donn at u.washington.edu
Wed Sep 5 00:24:24 CEST 2001


Quoth thp at cs.ucr.edu:

| I don't see why, say
|
|    if by_land():
|      lantern_count = 1
|    elif by_sea():
|      lantern_count = 2
|    else: 
|      lantern_count = 0
|
| should be more readable than, say
|
|    lantern_count = 
|      if    by_land() :  1 
|      elif  by_sea()  :  2 
|      else            :  0
|
| or simply
|
|    lantern_count = if by_land(): 1 elif by_sea(): 2 else: 0


No, next you'll want

lantern_count = if by_land(): (
     if by_bicycle(): 1 elif by_automobile(): 4 elif by_foot(): 5
   ) elif by_sea(): 2 else: 0

Now replace "0", "1" etc. with non-trivial computations.

| Lambda abstraction is a powerful feature that needs to be supported
| with a selection operator.  The ?: notation is not the only option
| for such a ternary operator.

Sure you can't just "def" an ordinary function with a name?

Scheme would be a great language to do stuff like that, I bet.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list