The "loop and a half"

Thomas Jollans tjol at tjol.eu
Wed Oct 4 19:15:19 EDT 2017


On 05/10/17 00:42, Stefan Ram wrote:
> Steve D'Aprano <steve+python at pearwood.info> writes:
>>> So, "bottom-up" in this case means: iterators should be
>>> taught before for-loops.
>> Why? 
>   The syntax for is (from memory):
>
> for <variable> in <expression>: <suite>
>
>   . As an example, I might show:
>
> for i in range( 3 ): ...
>
>   . This raises the question:
>
>       "What is the value of »range( 3 )«?".
>
>   "Bottom up" means that the simple and constituent parts
>   are taught before the complex and compound parts. I.e.,
>   »range( 3 )« is explained before »for i in range( 3 ):«.

If you apply that kind of thinking consistently with Python you'll get
caught in an infinite loop trying to explain attribute access before
even teaching print().

To call print, you need to access print.__call_. To access that
attribute, you need to check print.__dict__ (and some other things),
which is itself an attribute of print. To access that, (...)

>>> print.__class__.__dict__['__call__'].__call__(print, "hello world")
hello world
>>>

Of course that's not actually what happens when you call the builtin
print(). You can delve into the constituent parts of your favourite
Python implementation or the language reference to prove it, but that's
no longer simple.

"simple and constituent parts" is, quite often, not well-defined.
Iterators are pretty simple, and, while they have constituent parts,
pretty fundamental to Python, at least in spirit.

-- Thomas

>





More information about the Python-list mailing list