[Python-ideas] Framework for Python for CS101

Terry Reedy tjreedy at udel.edu
Mon May 25 23:52:45 CEST 2015


On 5/25/2015 8:11 AM, Rustom Mody wrote:

> I personally would wish for other minor surgeries eg a different keyword
> from 'def' for generators.

'Def' is for generator *functions*.  Guido notwithstanding, overloading 
'generator' to mean both a subcategory of function and the non-function 
iterators they produce leads to confusion.  The only structural 
difference between a normal function and generator function is a flag 
bit in the associated code object.

In 3.5, non-buggy generator functions must exit with explicit or 
implicit 'return', just as with other normal functions other than 
.__next__ methods.  Allowing generator functions to exit with 
StopIteration slightly confused them with iterator .__next__ methods.

>  From the pov of an experienced programmer the mental load of one
> keyword for two disparate purposes

The single purpose is to define a function object, with a defined set of 
attributes, that one may call.

  is easy enough to handle and the
> language clutter from an extra keyword is probably just not worth it.

An extra keyword 'async' is being added for coroutine functions, 
resulting I believe in 'async def'.  But I also believe this is not the 
only usage of 'async', while it would be the only usage of a 'gen' prefix.

> However from having taught python for 10+ years I can say this
> 'overloading' causes endless grief and slowdown of beginners.

I think part of the grief is overloading 'generator' to mean both a 
non-iterable function and an iterable non-function.

To really understand generators and generator functions, I think one 
needs to understand what an iterator class looks like, with a 
combination of boilerplate and custom code in .__init__, .__iter__, and 
.__next__.  The generator as iterator has the boilerplate code, while 
the generator function has the needed custom code in the .__init__ and 
.__next__ methods combined in one function body.  For this purpose, 
assignments between local and self attribute namespaces, which one might 
call 'custom boilerplate' are not needed and disappear.  One may think 
of a generator function as defining a subclass of the generator class.

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list