Alex, Wesley, Marion, and all instructors;<div><br></div><div>    To know Python down to the bytecode level as per Alex&#39;s email leaves me in awe.... (and reminds me of how much a newb I am with Python). Does anyone teach a class on this level? An advanced class?  I&#39;d *soo* want to take a course like that if/when it ever becomes available...  (as long as I have the prereq&#39;s of course -- I may not)... </div>
<div><br></div><div>    Throw in some Cython and other stuff and I&#39;m in heaven.</div><div><br></div><div>    Are any classes like this already scheduled?</div><div><br></div><div><br></div><div>Cheers,</div><div><br></div>
<div><br></div><div><br></div><div>Glen</div><div><br></div><div><div><br></div><div><br><div class="gmail_quote">On Mon, Jul 12, 2010 at 3:55 PM, Alex Martelli <span dir="ltr">&lt;<a href="mailto:aleax@google.com">aleax@google.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">`and` and `or` cannot be overridden; they always perform their<br>
short-circuit evaluation functionality, no matter what the types on<br>
either side.  I understand your surprise, because you think of them as<br>
operator, but they&#39;re like other keywords such as `lambda`, `if`,<br>
`else`... they **control what gets executed, when, and whether the<br>
execution happens at all in a certain circumstance**.  Consider:<br>
<br>
&gt;&gt;&gt; dis.dis(compile(&#39;x &amp; y&#39;, &#39;eval&#39;, &#39;eval&#39;))<br>
  1           0 LOAD_NAME                0 (x)<br>
              3 LOAD_NAME                1 (y)<br>
              6 BINARY_AND<br>
              7 RETURN_VALUE<br>
<br>
THIS is the semantics of an actual operator: BOTH operands get<br>
evaluated (here, just a LOAD_NAME for each), THEN the operator&#39;s code<br>
gets control (and can be overridden).  Vs:<br>
<br>
&gt;&gt;&gt; dis.dis(compile(&#39;x and y&#39;, &#39;eval&#39;, &#39;eval&#39;))<br>
  1           0 LOAD_NAME                0 (x)<br>
              3 JUMP_IF_FALSE            4 (to 10)<br>
              6 POP_TOP<br>
              7 LOAD_NAME                1 (y)<br>
        &gt;&gt;   10 RETURN_VALUE<br>
<br>
as you see, in bytecode terms, &#39;and&#39; means JUMP_IF_FALSE -- **no**<br>
Python-level code (potentially written by the user in an override)<br>
ever executes as part of this &quot;SO-CALLED&quot; operator -- which isn&#39;t<br>
really one, deep down: it&#39;s a *control structure*, like e.g.<br>
if/else... which ALSO cannot be overridden of course:<br>
<br>
&gt;&gt;&gt; dis.dis(compile(&#39;x if y else z&#39;, &#39;eval&#39;, &#39;eval&#39;))<br>
  1           0 LOAD_NAME                0 (y)<br>
              3 JUMP_IF_FALSE            5 (to 11)<br>
              6 POP_TOP<br>
              7 LOAD_NAME                1 (x)<br>
             10 RETURN_VALUE<br>
        &gt;&gt;   11 POP_TOP<br>
             12 LOAD_NAME                2 (z)<br>
             15 RETURN_VALUE<br>
<br>
Note the extreme similarity of this bytecode to that of the &#39;and&#39;<br>
so-called &quot;operator&quot; -- &#39;and&#39; is simpler, of course, but very much<br>
akin in semantics and implementation.<br>
<br>
You cannot override &#39;and&#39; and &#39;or&#39; just like you cannot override<br>
&#39;if/else&#39; or &#39;lambda&#39;.<br>
<font color="#888888"><br>
<br>
Alex<br>
</font><div><div></div><div class="h5"><br>
<br>
On Mon, Jul 12, 2010 at 3:01 PM, Stephen Lacy &lt;<a href="mailto:slacy@slacy.com">slacy@slacy.com</a>&gt; wrote:<br>
&gt; Ah, that Infix example is really cute, but not exactly what I was hoping<br>
&gt; for.<br>
&gt;<br>
&gt; I&#39;m actually playing with a bit of trickery -- the __eq__() operator doesn&#39;t<br>
&gt; need to return a boolean type (like I think __bool__() does). It can return<br>
&gt; any object.  You can use this trick to make __eq__() into a factory method<br>
&gt; and have it return an object that *represents* the comparison without<br>
&gt; actually *doing* the comparison.  This is useful for things like ORM<br>
&gt; mappers, where you take the Python expression, traverse the expression tree,<br>
&gt; translate it into SQL, and then execute it.  I&#39;m basing my ideas on the<br>
&gt; implementation of Column comparators in SQLAlchemy, which you can see some<br>
&gt; examples of here:<br>
&gt; <a href="http://www.sqlalchemy.org/docs/sqlexpression.html#operators" target="_blank">http://www.sqlalchemy.org/docs/sqlexpression.html#operators</a>  But I&#39;m not<br>
&gt; working with SQL, I&#39;m trying to do this for another query language.<br>
&gt;<br>
&gt; I&#39;m having trouble expressing &quot;and&quot; and &quot;or&quot; in a nice Pythonic way, and if<br>
&gt; you look at the SQLAlchemy source, they use and_() and or_() methods, which<br>
&gt; I&#39;d like to try to avoid. I was hoping for some trick, akin to the Infix<br>
&gt; thing you mentioned, but that would be a little more pythonic.  (And, I&#39;m<br>
&gt; surprised that you can override pretty much everything except for &#39;and&#39; and<br>
&gt; &#39;or&#39;)<br>
&gt;<br>
&gt; Here&#39;s a code example I wrote to get the juices flowing:<br>
&gt;<br>
&gt; <a href="http://dpaste.com/217476/" target="_blank">http://dpaste.com/217476/</a><br>
&gt;<br>
&gt; Line 60 is where things get interesting, I have to use bitwise &quot;and&quot; instead<br>
&gt; of boolean and, because as far as I can tell, there&#39;s no override for<br>
&gt; boolean and....<br>
&gt;<br>
&gt; Steve<br>
&gt;<br>
&gt; On Mon, Jul 12, 2010 at 2:14 PM, Brent Pedersen &lt;<a href="mailto:bpederse@gmail.com">bpederse@gmail.com</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; On Mon, Jul 12, 2010 at 11:21 AM, Stephen Lacy &lt;<a href="mailto:slacy@slacy.com">slacy@slacy.com</a>&gt; wrote:<br>
&gt;&gt; &gt; Hi,<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; According to <a href="http://docs.python.org/library/operator.html" target="_blank">http://docs.python.org/library/operator.html</a> boolean<br>
&gt;&gt; &gt; operators<br>
&gt;&gt; &gt; ==, &lt;, &gt;, etc. can be overridden via __eq__(), __lt__(), __gt__(), etc.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; They also mention *bitwise* and/or operators via __and__() and __or__().<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; But, I&#39;d like to override the *boolean* operators &#39;and&#39; and &#39;or&#39;.  Is<br>
&gt;&gt; &gt; this<br>
&gt;&gt; &gt; possible?<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Background:<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; I&#39;m experimenting with a library that takes a python expression like<br>
&gt;&gt; &gt; this:<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; &quot;(a == b) or (c and d)&quot;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; And instead of evaluating it via the interpreter, creates (and returns)<br>
&gt;&gt; &gt; a<br>
&gt;&gt; &gt; representation of said boolean decision tree such that it can be<br>
&gt;&gt; &gt; evaluated<br>
&gt;&gt; &gt; dynamically and/or converted to some other form, like prefix or postfix<br>
&gt;&gt; &gt; (this is just an example and for my own exploration and deeper<br>
&gt;&gt; &gt; understanding<br>
&gt;&gt; &gt; of operator overrides).<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; I can get this to work if I use the syntax &quot;(a == b) | (c &amp; d)&quot; but I<br>
&gt;&gt; &gt; would<br>
&gt;&gt; &gt; prefer to use the more pythonic and syntactically correct &quot;and&quot; and<br>
&gt;&gt; &gt; &quot;or&quot;.<br>
&gt;&gt; &gt; Is this possible?<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Steve<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; _______________________________________________<br>
&gt;&gt; &gt; Baypiggies mailing list<br>
&gt;&gt; &gt; <a href="mailto:Baypiggies@python.org">Baypiggies@python.org</a><br>
&gt;&gt; &gt; To change your subscription options or unsubscribe:<br>
&gt;&gt; &gt; <a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br>
&gt;&gt; &gt;<br>
&gt;&gt;<br>
&gt;&gt; not quite sure i follow your use-case, but...<br>
&gt;&gt; you can also override __nonzero__ which i think is what gets called<br>
&gt;&gt; when you use bool(),<br>
&gt;&gt; but you&#39;ll probably still need a bit more, you can add some sugar using<br>
&gt;&gt; this:<br>
&gt;&gt; <a href="http://code.activestate.com/recipes/384122-infix-operators/" target="_blank">http://code.activestate.com/recipes/384122-infix-operators/</a><br>
&gt;&gt;<br>
&gt;&gt; so you&#39;d have your classes implement __bool_or__ (or whatever you want<br>
&gt;&gt; to call it) and then use:<br>
&gt;&gt;<br>
&gt;&gt; OR = Infix(lambda a, b: a.__bool_or__(b))<br>
&gt;&gt;<br>
&gt;&gt; MyClass(22) |OR| MyClass(0)<br>
&gt;&gt;<br>
&gt;&gt; where MyClass implements __bool_or__<br>
&gt;&gt;<br>
&gt;&gt; -brent<br>
&gt;<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; Baypiggies mailing list<br>
&gt; <a href="mailto:Baypiggies@python.org">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>
&gt;<br>
_______________________________________________<br>
Baypiggies mailing list<br>
<a href="mailto:Baypiggies@python.org">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><br clear="all"><br>-- <br>Whatever you can do or imagine, begin it;<br>boldness has beauty, magic, and power in it.<br><br>-- Goethe <br>
</div></div>