[Python-ideas] Framework for Python for CS101

Terry Reedy tjreedy at udel.edu
Tue May 26 21:20:47 CEST 2015


On 5/26/2015 10:55 AM, Rustom Mody wrote:

> On Tuesday, May 26, 2015 at 7:16:18 PM UTC+5:30, Terry Reedy wrote:

>     The context is a beginning programming course where the goal is to
>     teach
>     people to write
>
>     def f(a, b, c): return a*b + c
>     print(f(2, 3, 4))
>
>     instead
>
>     def f(a, b, d): print(a*b + c)
>     f(2, 3, 4)
>
>     In other words, to teach beginners to relegate output to top level
>     code,
>     separate from the calculation code. (Or perhaps output functions, but
>     that is a more advanced topic.)  The first function is easy testable,
>     the second is not.

> Thanks Terry for the elucidation

>     For printing intermediate results, yield lines to top-level code that
>     can do whatever with them, including printing.
>
>     def text_generator(args):
>          ...
>              yield line
>
>     for line in text_generator: print(line)
>
>     is top-level code that prints intermediate results produced by a
>     testable generator.

> And thanks-squared for that.
> Generators are a really wonderful feature of python and not enough
> showcased.
> Think of lazy lists in haskell and how much fanfaring and trumpeting
> goes on around these.
> And by contrast how little of that for generators in the python world.
> Are the two all that different?
>
> You just have to think of all the data-structure/AI/etc books explaining
> depth-first-search and more arcane algorithms with a 'print' in the
> innards of it.
> And how far generators as a fundamental tool would go towards
> clarifying/modularizing these explanations
>
> So yes generators are an important component towards the goal of
> 'print-less' programming

I will just note that the stdlib is not immune from overly embedded 
prints.  Until 3.4, one could print a disassembly to stdout with 
dis.dis.  Period.  Hard for us to test; hard for others to use.  In 3.4, 
a file arg was added to dis, and the Bytecode and Instruction classes 
added, so one could a) iterate over unformatted named tuples, b) get the 
output as a string, or c) redirect the output to any 'file' (with a 
write method).  The result: easy for us to test; easy for others to use 
the data.

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list