classification
Title: Python tutorial could make rules for default argument definition clearer
Type: Stage:
Components: Documentation Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, jessevsilverman
Priority: normal Keywords:

Created on 2021-01-27 22:01 by jessevsilverman, last changed 2021-01-27 22:01 by jessevsilverman.

Messages (1)
msg385813 - (view) Author: Jesse Silverman (jessevsilverman) Date: 2021-01-27 22:01
I love the tutorial, but have been pointing out things I only knew from brutal experience, other tutorials, reasoning from other languages I know better, or looking things up.  I only mention them if they seem in scope and at the same level of the material which is presented.

Regarding 4.7.1. Default Argument Values of the excellent Python tutorial:

I guessed that I would see the behavior shown below from my understanding of default arguments in several other languages.  I did not read anything in this tutorial section that would lead me to conclude this, and other things mentioned in this section are neither much more or less subtle, more or less obvious, or more or less important.

PS F:\PythonCode> py VarArgs.py
  File "F:\PythonCode\VarArgs.py", line 1
    def varArgs(quote, footnote='', times):
                                         ^
SyntaxError: non-default argument follows default argument

Most introductions to default argument definition in other languages go out of their way to highlight that the default parameters can be only the last N of them, and that all required ones must come first.
Python is intended to be accessible to people no more clever than the other languages intend, so tho it seems obvious to me that they must only come after all required arguments, I remember when it was not obvious (and fortunately the tutorials I read in the past for other languages belabored the point).

The explanatory text of:
giving one of the optional arguments: ask_ok('OK to overwrite the file?', 2)

as it merely says "one of", rather than "the first of" led me to wonder if Python was somehow more flexible/better at mind-reading than the languages I already know well that require all default args to be trailing, and to only let you default the last one, two, or ... N.  The results of the experiment shown above demonstrate it is similar to the behavior in other languages in this regard.

I deeply appreciate the information about when the default arguments get evaluated, which is highly relevant and is not something I would have picked up on from other languages I've used.

A simple line stating that "any default arguments must follow any required arguments in the definition" and something like "on calls, optional arguments may only be omitted from the end of the parameter list, they can't simply be skipped" would be awesome.

I am not sure whether or not this is made perfectly clear in the next section, which has a lot of other new and interesting ideas flying around in it as well, besides this one.
History
Date User Action Args
2021-01-27 22:01:00jessevsilvermancreate