Regarding the magical "or" operator

Georg Mischler schorsch at schorsch.com
Sun Aug 15 06:55:50 EDT 1999


Tom Christiansen wrote:
[...]
> My question is this: have you ever had an urge for it (or a variant)
> to care about the difference between None and any other false value?
> I am certainly *not* asking for such a thing.  I'd just like to know
> whether anyone ever cared.
[...]

Yes, this has happened to me a few times. The usual setup is a
function that normally either returns a list or a numerical value,
but can fail in some circumstances, and where, obviously, [] or 0
are valid return values. In most cases, another look at the problem
will reveal a missed  opportunity to throw an exception for the
failed case.

There may be situations where an exception would at least *look*
like too much overhead. But with the current semantics, the extra
check for None is not really simpler or faster to type.
With your unproposed "nor" operator, the situation would change
a little. But then, even this would only apply to the special
case where the resulting value is used in an assignment.
As soon as other decisions depend on this result, the standard
exception handling seems to result in the most readable code,
especially in combination with the "else" and "finally" clauses
after "try" and "except".

And yes, I recently have started to use "or" and "and" as
assignment value selectors, as well as the abovementioned "else"
in exception handling and in looping constructs. It doesn't happen
very often, but can simplify the code a lot in some situations.
Given my lisp background, it's actually surprising that I lived
without noticing those possibilities for my first few years
of Python exposure...


Have fun!

-schorsch

--
Georg Mischler  --  simulation developper  --  schorsch at schorsch.com
+schorsch.com+  --  lighting design tools  --  http://www.schorsch.com/


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




More information about the Python-list mailing list