Author paul.j3
Recipients Gabriel Devenyi, Markus.Amalthea.Magnuson, SylvainDe, Yclept.Nemo, bethard, docs@python, michal.klich, paul.j3, r.david.murray
Date 2016-10-02.23:05:19
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1475449519.43.0.914153568745.issue16399@psf.upfronthosting.co.za>
In-reply-to
Content
It may help to know something about how defaults are handled - in general.

`add_argument` and `set_defaults` set the `default` attribute of the Action (the object created by `add_argument` to hold all of its information).  The default `default` is `None`.

At the start of `parse_args`, a fresh Namespace is created, and all defaults are loaded into it (I'm ignoring some details).

The argument strings are then parsed, and individual Actions update the Namespace with their values, via their `__call__` method.

At the end of parsing it reviews the Namespace.  Any remaining defaults that are strings are evaluated (passed through `type` function that converts a commandline string).  The handling of defaults threads a fine line between giving you maximum power, and keeping things simple and predictable.

The important thing for this issue is that the defaults are loaded into the Namespace at the start of parsing.

The `append` call fetches the value from the Namespace, replaces it with `[]` if it is None, appends the new value(s), and puts it back on the Namespace.  The first `--foo` append is handled in just the same way as the 2nd and third (fetch, append, and put back).  The first can't tell that the list it fetches from the namespace came from the `default` as opposed to a previous `append`.  

The `__call__` for `append` was intentionally kept simple, and predictable.  As I demonstrated earlier it is possible to write an `append` that checks the namespace value against some default, and does something different.  But that is more complicated.

The simplest alternative to this behavior is to leave the default as None.  If after parsing the value is still None, put the desired list (or any other object) there.  

The primary purpose of the parser is to parse the commandline - to figure out what the user wants to tell you.  There's nothing wrong with tweaking (and checking) the `args` Namespace after parsing.
History
Date User Action Args
2016-10-02 23:05:19paul.j3setrecipients: + paul.j3, bethard, r.david.murray, docs@python, Yclept.Nemo, Markus.Amalthea.Magnuson, SylvainDe, michal.klich, Gabriel Devenyi
2016-10-02 23:05:19paul.j3setmessageid: <1475449519.43.0.914153568745.issue16399@psf.upfronthosting.co.za>
2016-10-02 23:05:19paul.j3linkissue16399 messages
2016-10-02 23:05:19paul.j3create