[Baypiggies] overriding boolean and,or operators?

Aahz aahz at pythoncraft.com
Tue Jul 13 01:48:58 CEST 2010


On Mon, Jul 12, 2010, Alex Martelli wrote:
>
> `and` and `or` cannot be overridden; they always perform their
> short-circuit evaluation functionality, no matter what the types on
> either side.  I understand your surprise, because you think of them as
> operator, but they're like other keywords such as `lambda`, `if`,
> `else`... they **control what gets executed, when, and whether the
> execution happens at all in a certain circumstance**.  

Just to emphasize Alex's point, this is a necessary part of the boolean
short-circuit capability that allows stuff like this:

    if a is not None and a.valid():

(assume that a contains a method called "valid" if it's not None -- but
there's no getattr() for "valid" unless a is not None)
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"....Normal is what cuts off your sixth finger and your tail..."  --Siobhan


More information about the Baypiggies mailing list