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