Python for philosophers

llanitedave llanitedave at veawb.coop
Sun May 12 14:13:30 EDT 2013


On Sunday, May 12, 2013 7:51:28 AM UTC-7, Chris Angelico wrote:
> On Mon, May 13, 2013 at 12:17 AM, Citizen Kant <citizenkant at gmail.com> wrote:
> 
> > Maybe It'd be good if I explain myself a bit more. What I'm trying here is
> 
> > to grasp Python from the game's abstraction point of view, as if it were,
> 
> > for example, chess.
> 
> 
> 
> Maybe you're going for something a little too complicated. Let's boil
> 
> the question down to a much MUCH simpler subset: expression
> 
> evaluation. And let's take Python itself right out of the picture, and
> 
> work with only the following elements:
> 
> 
> 
> * Parentheses ( )
> 
> * Multiplication and division * /
> 
> * Addition and subtraction + -
> 
> * Decimal integer constants 0123456789
> 
> 
> 
> This will give the basics of algebraic evaluation. It's quite fun to
> 
> build a simple little expression evaluator that just does these few
> 
> operations, and it's surprisingly useful (embed it within a command
> 
> interpreter, for instance). Some time ago I wrote one into a Windows
> 
> app (written in C++), and when I pulled it out just now and made it a
> 
> stand-alone tool, the whole thing was only ~60 lines of code. Nice and
> 
> easy to work with.
> 
> 
> 
> So, let's take an expression and see what it really means.
> 
> 
> 
> 2*(3+4)+6
> 
> 
> 
> One way to interpret this is with a stack. Here's how Python evaluates
> 
> that expression:
> 
> 
> 
> >>> def foo():
> 
> 	return 2*(three+4)+6
> 
> 
> 
> >>> dis.dis(foo)
> 
>   2           0 LOAD_CONST               1 (2)
> 
>               3 LOAD_GLOBAL              0 (three)
> 
>               6 LOAD_CONST               2 (4)
> 
>               9 BINARY_ADD
> 
>              10 BINARY_MULTIPLY
> 
>              11 LOAD_CONST               3 (6)
> 
>              14 BINARY_ADD
> 
>              15 RETURN_VALUE
> 
> 
> 
> (I had to replace one of the constants with a global, to foil the optimizer)
> 
> 
> 
> The LOAD statements add to an internal stack, the BINARY operations
> 
> pop two operands and push the result. This is more-or-less the same
> 
> technique as I used in my evaluator, except that instead of compiling
> 
> it to code, I just march straight through, left to right, and so I had
> 
> to maintain two stacks (one of operands, one of operators).
> 
> 
> 
> Is this what you had in mind when you wanted to grasp Python's
> 
> internals? Because it's pretty easy to explore, thanks to the dis
> 
> module. There's a huge amount that can be learned about the
> 
> interpreter, its optimizers, and so on, just by disassembling
> 
> functions. Alternatively, if you're thinking on a more abstract level,
> 
> leave Python aside altogether and pick up a book on language design.
> 
> Also can be awesome fun, but completely different.
> 
> 
> 
> ChrisA

No, that won't help.  You're trying to give him knowledge; he wants understanding.



More information about the Python-list mailing list