Sure--I'd be up for that. <br><br>-Damon<br><br><br><div class="gmail_quote">On Mon, Jan 25, 2010 at 8:24 PM, jim <span dir="ltr"><<a href="mailto:jim@well.com" target="_blank">jim@well.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
i think the examples in this thread are pretty good.<br>
you and i gave it a shot, glen; it'd be interesting to<br>
listen to Damon's pitch.<br>
what say, Damon: want to do a newbie nugget at one of<br>
the bayPIGgies' meetings?<br>
<div><div></div><div><br>
<br>
<br>
On Mon, 2010-01-25 at 17:37 -0800, Glen Jarvis wrote:<br>
> Agreed. Jim prepared a similar newbie nugget about chaining, but had a<br>
> toothache and couldn't present it. I did at the last minute, but don't<br>
> think I sid it justice.<br>
><br>
><br>
> Jim, what are the chances you present again with some of these<br>
> examples. Or, Max. Or anyone. I'd like to learn more.<br>
><br>
><br>
> Cheers,<br>
><br>
><br>
> Glen<br>
><br>
> On Jan 25, 2010, at 4:06 PM, Max Slimmer <<a href="mailto:max@theslimmers.net" target="_blank">max@theslimmers.net</a>> wrote:<br>
><br>
><br>
><br>
> > Great explanation, this should be a good newbie nugget.<br>
> ><br>
> > Thanks,<br>
> > Max Slimmer<br>
> ><br>
> ><br>
> ><br>
> > On Mon, Jan 25, 2010 at 3:03 PM, Damon McCormick <<a href="mailto:damonmc@gmail.com" target="_blank">damonmc@gmail.com</a>><br>
> > wrote:<br>
> > While the suggested *fixes* are all correct (when in doubt,<br>
> > explicilty parenthesize!), none of the *explanations* for<br>
> > the unexpected output are quite right. Since this involves<br>
> > a subtle issue, I thought I'd send a full explanation.<br>
> ><br>
> > It's tempting to assume that<br>
> ><br>
> > >>> a in alist == b in alist<br>
> > is equivalent to<br>
> > >>> ((a in alist) == b) in alist<br>
> ><br>
> > However, this is not correct!<br>
> ><br>
> > For a simpler (but perhaps more confusing) example of the<br>
> > hazards of using "in" and "==", non-parenthesized, in an<br>
> > expression like this, consider the following:<br>
> ><br>
> > >>> a = 1 # as before<br>
> > >>> alist = [5,6] # as before<br>
> > >>> a in alist == False<br>
> > False<br>
> > >>> (a in alist) == False<br>
> > True<br>
> ><br>
> > Weird, right? And no, putting parens around (alist ==<br>
> > False) won't work--that would be an exception because the<br>
> > right side of the 'in' operator wouldn't be iterable.<br>
> ><br>
> > Here's one last example:<br>
> ><br>
> > >>> blist = [1, [5,6]]<br>
> > >>> 5 in alist == [5,6] in blist<br>
> > True<br>
> ><br>
> > You might enjoy the exercise of figuring out why the above<br>
> > output is correct. But to cut to the chase, what's going on<br>
> > is the following. Python allows comparisons to be chained,<br>
> > as in the following:<br>
> ><br>
> > >>> a == 1 == 2/2<br>
> > True<br>
> > >>> 1 < 5 < 7<br>
> > True<br>
> ><br>
> > The way the chaining works (see 5.9 in<br>
> > <a href="http://tinyurl.com/3vsb6m" target="_blank">http://tinyurl.com/3vsb6m</a>) is that<br>
> ><br>
> > >>> a == 1 == 2/2<br>
> > is equivalent to<br>
> > >>> (a == 1) and (1 == 2/2)<br>
> ><br>
> > and<br>
> ><br>
> > >>> 1 < 5 < 7<br>
> > is equivalent to<br>
> > >>> (1 < 5) and (5 < 7)<br>
> ><br>
> > Since 'in' is just another comparison operator, it works the<br>
> > same way. Thus, the the original expression<br>
> ><br>
> > >>> a in alist == b in alist<br>
> > is equivalent to<br>
> > >>>(a in alist) and (alist == b) and (b in alist)<br>
> ><br>
> > which is False because all three comparisons are False.<br>
> > You'll see that the two other examples I came up with make<br>
> > sense in this context as well.<br>
> ><br>
> > -Damon<br>
> ><br>
> ><br>
> ><br>
> > On Mon, Jan 25, 2010 at 1:27 PM, Asher Langton<br>
> > <<a href="mailto:langton2@llnl.gov" target="_blank">langton2@llnl.gov</a>> wrote:<br>
> > On Jan 25, 2010, at 1:12 PM, Max Slimmer wrote:<br>
> ><br>
> ><br>
> > Can anyone explain the following:<br>
> ><br>
> > >>> a = 1<br>
> > >>> b = 2<br>
> > >>> alist = [5,6]<br>
> > >>> print a in alist<br>
> > False<br>
> ><br>
> > >>> a in alist == b in alist<br>
> > False<br>
> > >>> a in alist == a in alist<br>
> > False<br>
> > >>> bool(a in alist) == bool(b in alist)<br>
> > # this does what we expect<br>
> > True<br>
> > >>> c = 5<br>
> > >>> c in alist == c in alist<br>
> > False<br>
> > >>><br>
> ><br>
> ><br>
> > The '==' and 'in' operators have the same<br>
> > precedence, so the expression 'a in alist == b in<br>
> > alist' is evaluated left-to-right as:<br>
> ><br>
> > >>> ( (a in alist) == b) in alist<br>
> ><br>
> > Since 'a in alist' is False, this is the same as<br>
> ><br>
> > >>> ( False == b) in alist<br>
> ><br>
> > which can be simplified to<br>
> ><br>
> > >>> False in alist<br>
> ><br>
> > which is False.<br>
> ><br>
> ><br>
> > -Asher<br>
> ><br>
> ><br>
> > _______________________________________________<br>
> > Baypiggies mailing list<br>
> > <a href="mailto:Baypiggies@python.org" target="_blank">Baypiggies@python.org</a><br>
> > To change your subscription options or unsubscribe:<br>
> > <a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br>
> ><br>
> ><br>
> ><br>
> ><br>
> > _______________________________________________<br>
> > Baypiggies mailing list<br>
> > <a href="mailto:Baypiggies@python.org" target="_blank">Baypiggies@python.org</a><br>
> > To change your subscription options or unsubscribe:<br>
> > <a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br>
> _______________________________________________<br>
> Baypiggies mailing list<br>
> <a href="mailto:Baypiggies@python.org" target="_blank">Baypiggies@python.org</a><br>
> To change your subscription options or unsubscribe:<br>
> <a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br>
<br>
_______________________________________________<br>
Baypiggies mailing list<br>
<a href="mailto:Baypiggies@python.org" target="_blank">Baypiggies@python.org</a><br>
To change your subscription options or unsubscribe:<br>
<a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br>
</div></div></blockquote></div><br>