[Python-3000] Wild idea: Deferred Evaluation & Implicit Lambda

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Jun 2 03:03:07 CEST 2006


Toby Dickenson wrote:

> The ?? operator first evaluated its left operand. If that succeeds its value 
> is returned. If that raised an exception it evaluates and returns its right 
> operand. That allowed your example to be written:
> 
> 	value = a[key] ?? b[key] ?? 0

That wouldn't make sense so much in Python, because you
don't usually want to catch all exceptions, only particular
ones. So the operator would need to be parameterised
somehow with the exception to catch, which would make
it much less concise.

A more Pythonic way would be something like

   value = a.get(key) or b.get(key) or 0

If some of your legitimate values can be false, you
might need to use functions that attempt to get a
value and return it wrapped somehow.

The thought has just occurred that what *might* be
useful here is an operator that works like "or",
except that the only value it recognises as "false"
is None. Not sure what to call it, though...

--
Greg




More information about the Python-3000 mailing list