classification
Title: TypeError for Tutorial 10.3 Example 2
Type: behavior Stage: needs patch
Components: Documentation Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: David Goldsmith, docs@python, rhettinger, terry.reedy
Priority: normal Keywords:

Created on 2019-11-03 23:31 by David Goldsmith, last changed 2019-11-09 08:13 by rhettinger.

Messages (3)
msg355914 - (view) Author: David Goldsmith (David Goldsmith) Date: 2019-11-03 23:31
When I run the second example code of Section 10.3 of The Python Tutorial:

import argparse
from getpass import getuser
parser = argparse.ArgumentParser(description='An argparse example.')
parser.add_argument('name', nargs='?', default=getuser(), help='The name of someone to greet.')
parser.add_argument('--verbose', '-v', action='count')
args = parser.parse_args()
greeting = ["Hi", "Hello", "Greetings! its very nice to meet you"][args.verbose % 3]
print(f'{greeting}, {args.name}')
if not args.verbose:
    print('Try running this again with multiple "-v" flags!')

the greeting assignment line raises:

>>> greeting = ["Hi", "Hello", "Greetings! its very nice to meet you"][args.verbose % 3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'
 
Is this an artifact of me being so far behind in my Python version:

Davids-MacBook-Air:~ DLG$ python3
Python 3.4.3 |Anaconda 2.4.0 (x86_64)| (default, Oct 20 2015, 14:27:51) 
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin

Or is it a bug in the example code in the doc?
msg356273 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-11-08 23:29
Crash means 'python stopped erroneously without a traceback'. 

Same exception in 3.8 and 3.9.  Argparse uses None for a count of 0.  I consider this a bug.  If not changed, it should be documented.  As argparse is, the example needs to recode None to 0.  (Or, it could add '-v' to sys.argv.)  With proper line wrapping, the result could be

import argparse
from getpass import getuser
parser = argparse.ArgumentParser(description='An argparse example.')
parser.add_argument('name', nargs='?', default=getuser(),  # wrap
                    help='The name of someone to greet.')
parser.add_argument('--verbose', '-v', action='count')
args = parser.parse_args()  # new
args.verbose = 0 if args.verbose is None else args.verbose
greeting = (["Hi", "Hello", "Greetings! its very nice to meet you"] #wrap
            [args.verbose % 3])
print(f'{greeting}, {args.name}')
msg356291 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-11-09 08:13
I'll replace this example with more representative one that works.
History
Date User Action Args
2019-11-09 08:13:26rhettingersetassignee: docs@python -> rhettinger

messages: + msg356291
nosy: + rhettinger
2019-11-08 23:29:23terry.reedysetnosy: + terry.reedy
title: TypeError raised trying to run TPT 10.3 Example 2 in Python 3.4.3 -> TypeError for Tutorial 10.3 Example 2
messages: + msg356273

type: crash -> behavior
stage: needs patch
2019-11-03 23:31:18David Goldsmithcreate