classification
Title: Error in functional how-to example
Type: Stage: needs patch
Components: Documentation Versions: Python 3.9, Python 3.8, Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, dxflores, eric.smith, terry.reedy
Priority: normal Keywords:

Created on 2020-03-15 16:39 by dxflores, last changed 2020-03-20 19:44 by terry.reedy.

Messages (6)
msg364247 - (view) Author: Diogo Flores (dxflores) Date: 2020-03-15 16:39
Hello,

A code example from the 'functional programming how-to' raises an error.

The simplest fix would be to remove the Ellipsis object from the 'line_list'.

Thank you,
Diogo

Please check below for the commands issued:

# https://docs.python.org/3/howto/functional.html#generator-expressions-and-list-comprehensions

>>> line_list = ['  line 1\n', 'line 2  \n', ...]
>>> 
>>> # Generator expression -- returns iterator
>>> stripped_iter = (line.strip() for line in line_list)
>>> 
>>> # List comprehension -- returns list
>>> stripped_list = [line.strip() for line in line_list]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <listcomp>
AttributeError: 'ellipsis' object has no attribute 'strip'
msg364275 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-03-16 01:46
I don't think that was meant to be a literal example. I think that "..." here means "and other strings".
msg364346 - (view) Author: Diogo Flores (dxflores) Date: 2020-03-16 17:50
Agreed, Eric. Nevertheless, I think that this example might raise unnecessary confusion to someone new to Python, and hence the reason why I decided to open the issue. Anyhow, I trust your judgement and please feel free to leave things as is and close this issue.

PS: I really the work you did with dataclasses, and I actually explored the idea further with a small (300 LoC) library that adds runtime type checking for instances of dataclasses - if you are ever curious you can find the code on my GitHub at /dxflores/invis.

Thank you,
Diogo
msg364708 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-03-20 19:17
In think examples should run and that '...' should be replaced by something like "' \n', ''".  The blank string looks ahead to the next example, which adds ' if line != ""' to the comprehension.
msg364709 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-03-20 19:25
Working on PR.  The example 1 further on has a stray meaningless ellipsis.

             for expr3 in sequence3 ...
             if condition3

Perhaps this was meant to follow the next line, but I will remove it.
msg364710 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-03-20 19:44
Looking around further, examples meant to be executable as is are given interactive prompts and needed doctest directives

    >>> gen  #doctest: +ELLIPSIS
    <generator object generate_ints at ...>

I think that this example should get the same treatment.  Non-executable examples are usually clearly non-executable because then contain undefined names line 'expr2' and 'sequence2', whereas this example starts by defining 'line_list'.
History
Date User Action Args
2020-03-20 19:44:24terry.reedysetmessages: + msg364710
2020-03-20 19:25:10terry.reedysetmessages: + msg364709
2020-03-20 19:17:32terry.reedysetnosy: + terry.reedy
title: Error on documentation - Quick fix. -> Error in functional how-to example
messages: + msg364708

versions: + Python 3.7, Python 3.9
stage: needs patch
2020-03-16 17:50:37dxfloressetmessages: + msg364346
2020-03-16 01:46:10eric.smithsetnosy: + eric.smith
messages: + msg364275
2020-03-15 16:39:23dxflorescreate