[docs] [issue23356] In argparse docs simplify example about argline

py.user report at bugs.python.org
Sat Jan 31 09:40:39 CET 2015


New submission from py.user:

The example is:

def convert_arg_line_to_args(self, arg_line):
    for arg in arg_line.split():
        if not arg.strip():
            continue
        yield arg

str.split() with default delimiters never returns empty or whitespace strings in the list.

>>> '  x  x  '.split()
['x', 'x']
>>> '  '.split()
[]
>>>

Therefore, the if condition doesn't ever continue the loop.
It can be written:

def convert_arg_line_to_args(self, arg_line):
    for arg in arg_line.split():
        yield arg

It's the same as:

def convert_arg_line_to_args(self, arg_line):
    return iter(arg_line.split())

I guess, nothing uses next() for the result:

def convert_arg_line_to_args(self, arg_line):
    return arg_line.split()

Applied a patch with the last variant.

----------
assignee: docs at python
components: Documentation, Library (Lib)
files: args_ex_argline.diff
keywords: patch
messages: 235089
nosy: docs at python, py.user
priority: normal
severity: normal
status: open
title: In argparse docs simplify example about argline
type: performance
versions: Python 2.7, Python 3.4
Added file: http://bugs.python.org/file37934/args_ex_argline.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue23356>
_______________________________________


More information about the docs mailing list