[Baypiggies] overriding boolean and,or operators?
Jeremy Fishman
jeremy.r.fishman at gmail.com
Tue Jul 13 02:37:19 CEST 2010
Wow, thanks. It's neat to see the difference between
>>> dis.dis(compile('x if y else z', 'eval', 'eval'))
1 0 LOAD_NAME 0 (y)
3 JUMP_IF_FALSE 5 (to 11)
6 POP_TOP
7 LOAD_NAME 1 (x)
10 RETURN_VALUE
>> 11 POP_TOP
12 LOAD_NAME 2 (z)
15 RETURN_VALUE
>>> dis.dis(compile('y and x or z', 'eval', 'eval'))
1 0 LOAD_NAME 0 (y)
3 JUMP_IF_FALSE 7 (to 13)
6 POP_TOP
7 LOAD_NAME 1 (x)
10 JUMP_IF_TRUE 4 (to 17)
>> 13 POP_TOP
14 LOAD_NAME 2 (z)
>> 17 RETURN_VALUE
in byte code. Now if only I weren't stuck in 2.4.3 at work...
- Jeremy
On Mon, Jul 12, 2010 at 4:48 PM, Aahz <aahz at pythoncraft.com> wrote:
> 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
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20100712/61a538b7/attachment.html>
More information about the Baypiggies
mailing list