Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45184 > unrolled thread

Re: Python for philosophers

Started byChris Angelico <rosuav@gmail.com>
First post2013-05-13 00:51 +1000
Last post2013-05-13 07:48 +1000
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Python for philosophers Chris Angelico <rosuav@gmail.com> - 2013-05-13 00:51 +1000
    Re: Python for philosophers llanitedave <llanitedave@veawb.coop> - 2013-05-12 11:13 -0700
      Re: Python for philosophers Chris Angelico <rosuav@gmail.com> - 2013-05-13 07:48 +1000

#45184 — Re: Python for philosophers

FromChris Angelico <rosuav@gmail.com>
Date2013-05-13 00:51 +1000
SubjectRe: Python for philosophers
Message-ID<mailman.1582.1368370291.3114.python-list@python.org>
On Mon, May 13, 2013 at 12:17 AM, Citizen Kant <citizenkant@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

[toc] | [next] | [standalone]


#45193

Fromllanitedave <llanitedave@veawb.coop>
Date2013-05-12 11:13 -0700
Message-ID<6e40860b-c0e5-46f9-9ba9-4ea4f4dc77c2@googlegroups.com>
In reply to#45184
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@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.

[toc] | [prev] | [next] | [standalone]


#45202

FromChris Angelico <rosuav@gmail.com>
Date2013-05-13 07:48 +1000
Message-ID<mailman.1597.1368395317.3114.python-list@python.org>
In reply to#45193
On Mon, May 13, 2013 at 4:13 AM, llanitedave <llanitedave@veawb.coop> wrote:
> 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@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.

I can't give him understanding. Best I can do is offer facts, which
lead to knowledge, which lead to understanding if absorbed
appropriately. Also, sources of knowledge (like dis.dis) can be VERY
powerful tools in gaining understanding.

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web