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

Roman Suzi rnd at onego.ru
Sun Feb 9 18:10:07 CET 2003


Let me try idea-generating...

I think inline if-construct must be (if it is to be at all) analogous
to if-elif-...-elif-else.

And my imagination fails on how to make it syntactically...
Do not forget ambiguity of if-then-else without bracketing!

Unless lazily evaluated function arguments are introduced,
there is no other way to introduce inline if.


def my_if(cond, ***true, ***false):
  if cond:
    return true.eval()
  else:
    return false.eval()


print my_if(2 < 3, big_fun1(), big_fun2())


If is even possible today:

def big_fun1():
  return 1

def big_fun2():
  return 2

def my_if(cond, true, false):
  if cond: return true()
  else: return false()


print my_if(2 < 3, lambda:big_fun1(), lambda:big_fun2())
print my_if(345 < 123, lambda:"yes", lambda:"no")

The main problem of Python is 6-letter lambda. What if we
replace it with "?" ;-)

print my_if(345 < 123, ?:"yes", ?:"no")

Or !:

print my_if(345 < 123, !:"yes", !:"no")

Some other good uses:

print reduce(!x,y: x+1, [1,2,3,4], 0)

Uh-uh... Who says lambda is ugly?

Same goes for the other possible uses, like code thunks:

A = B() !:
   suite


In short:

print my_if(ternary_if_needed, !:"yes", !:"no", !:"no way", !:"exception")


Sincerely yours, Roman Suzi
-- 
rnd at onego.ru =\= My AI powered by Linux RedHat 7.3






More information about the Python-list mailing list