Alex, Wesley, Marion, and all instructors;<div><br></div><div> To know Python down to the bytecode level as per Alex'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'd *soo* want to take a course like that if/when it ever becomes available... (as long as I have the prereq's of course -- I may not)... </div>
<div><br></div><div> Throw in some Cython and other stuff and I'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"><<a href="mailto:aleax@google.com">aleax@google.com</a>></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'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>
>>> dis.dis(compile('x & y', 'eval', 'eval'))<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's code<br>
gets control (and can be overridden). Vs:<br>
<br>
>>> dis.dis(compile('x and y', 'eval', 'eval'))<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>
>> 10 RETURN_VALUE<br>
<br>
as you see, in bytecode terms, 'and' means JUMP_IF_FALSE -- **no**<br>
Python-level code (potentially written by the user in an override)<br>
ever executes as part of this "SO-CALLED" operator -- which isn't<br>
really one, deep down: it's a *control structure*, like e.g.<br>
if/else... which ALSO cannot be overridden of course:<br>
<br>
>>> dis.dis(compile('x if y else z', 'eval', 'eval'))<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>
>> 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 'and'<br>
so-called "operator" -- 'and' is simpler, of course, but very much<br>
akin in semantics and implementation.<br>
<br>
You cannot override 'and' and 'or' just like you cannot override<br>
'if/else' or 'lambda'.<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 <<a href="mailto:slacy@slacy.com">slacy@slacy.com</a>> wrote:<br>
> Ah, that Infix example is really cute, but not exactly what I was hoping<br>
> for.<br>
><br>
> I'm actually playing with a bit of trickery -- the __eq__() operator doesn't<br>
> need to return a boolean type (like I think __bool__() does). It can return<br>
> any object. You can use this trick to make __eq__() into a factory method<br>
> and have it return an object that *represents* the comparison without<br>
> actually *doing* the comparison. This is useful for things like ORM<br>
> mappers, where you take the Python expression, traverse the expression tree,<br>
> translate it into SQL, and then execute it. I'm basing my ideas on the<br>
> implementation of Column comparators in SQLAlchemy, which you can see some<br>
> examples of here:<br>
> <a href="http://www.sqlalchemy.org/docs/sqlexpression.html#operators" target="_blank">http://www.sqlalchemy.org/docs/sqlexpression.html#operators</a> But I'm not<br>
> working with SQL, I'm trying to do this for another query language.<br>
><br>
> I'm having trouble expressing "and" and "or" in a nice Pythonic way, and if<br>
> you look at the SQLAlchemy source, they use and_() and or_() methods, which<br>
> I'd like to try to avoid. I was hoping for some trick, akin to the Infix<br>
> thing you mentioned, but that would be a little more pythonic. (And, I'm<br>
> surprised that you can override pretty much everything except for 'and' and<br>
> 'or')<br>
><br>
> Here's a code example I wrote to get the juices flowing:<br>
><br>
> <a href="http://dpaste.com/217476/" target="_blank">http://dpaste.com/217476/</a><br>
><br>
> Line 60 is where things get interesting, I have to use bitwise "and" instead<br>
> of boolean and, because as far as I can tell, there's no override for<br>
> boolean and....<br>
><br>
> Steve<br>
><br>
> On Mon, Jul 12, 2010 at 2:14 PM, Brent Pedersen <<a href="mailto:bpederse@gmail.com">bpederse@gmail.com</a>> wrote:<br>
>><br>
>> On Mon, Jul 12, 2010 at 11:21 AM, Stephen Lacy <<a href="mailto:slacy@slacy.com">slacy@slacy.com</a>> wrote:<br>
>> > Hi,<br>
>> ><br>
>> > According to <a href="http://docs.python.org/library/operator.html" target="_blank">http://docs.python.org/library/operator.html</a> boolean<br>
>> > operators<br>
>> > ==, <, >, etc. can be overridden via __eq__(), __lt__(), __gt__(), etc.<br>
>> ><br>
>> > They also mention *bitwise* and/or operators via __and__() and __or__().<br>
>> ><br>
>> > But, I'd like to override the *boolean* operators 'and' and 'or'. Is<br>
>> > this<br>
>> > possible?<br>
>> ><br>
>> > Background:<br>
>> ><br>
>> > I'm experimenting with a library that takes a python expression like<br>
>> > this:<br>
>> ><br>
>> > "(a == b) or (c and d)"<br>
>> ><br>
>> > And instead of evaluating it via the interpreter, creates (and returns)<br>
>> > a<br>
>> > representation of said boolean decision tree such that it can be<br>
>> > evaluated<br>
>> > dynamically and/or converted to some other form, like prefix or postfix<br>
>> > (this is just an example and for my own exploration and deeper<br>
>> > understanding<br>
>> > of operator overrides).<br>
>> ><br>
>> > I can get this to work if I use the syntax "(a == b) | (c & d)" but I<br>
>> > would<br>
>> > prefer to use the more pythonic and syntactically correct "and" and<br>
>> > "or".<br>
>> > Is this possible?<br>
>> ><br>
>> > Steve<br>
>> ><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>
>> ><br>
>><br>
>> not quite sure i follow your use-case, but...<br>
>> you can also override __nonzero__ which i think is what gets called<br>
>> when you use bool(),<br>
>> but you'll probably still need a bit more, you can add some sugar using<br>
>> this:<br>
>> <a href="http://code.activestate.com/recipes/384122-infix-operators/" target="_blank">http://code.activestate.com/recipes/384122-infix-operators/</a><br>
>><br>
>> so you'd have your classes implement __bool_or__ (or whatever you want<br>
>> to call it) and then use:<br>
>><br>
>> OR = Infix(lambda a, b: a.__bool_or__(b))<br>
>><br>
>> MyClass(22) |OR| MyClass(0)<br>
>><br>
>> where MyClass implements __bool_or__<br>
>><br>
>> -brent<br>
><br>
><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>
><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>