
Function prototype shows fieldnames as a positional parameter but usage example shows it as a keyword argument. https://docs.python.org/3/library/csv.html#csv.DictWriter

Hi Bill! In the following syntax: class csv.DictWriter(f, fieldnames, restval='', extrasaction='raise', dialect='excel', *args, **kwds) I bet what make you think f and fieldnames are by position and not by name may be the fact that they hold no default values. But this is in fact not directly related. This is true that the set of arguments holding default values are optional, and this is true that to avoid listing all optional values when calling something there's a strong and good habit of giving optional arguments by name. But it's just a good habit, there's nothing enforced by the syntax (it can be enforced by using / and * but they are not used here). So all of the following are valid calls: DictWriter(f=csvfile, fieldnames=fieldnames) DictWriter(csvfile, fieldnames) DictWriter(fieldnames=fieldnames, f=csvfile) DictWriter(csvfile, fieldnames=fieldnames) I think the first one is too verbose while not being helpfull: `f` don't give any relevant information. The 3rd one is no better, inversing the order of argument is just misleading here. So the 2nd and the last one are valid, and only the "good taste" of a developer can tell which one is the more readable. In the case the field names are stored in a `fieldnames` variable, maybe `fieldnames=fieldnames` is a bit redundent and the 2nd one is the best. But in the case the field names are given literally, maybe DictWriter(csvfile, fieldnames=["id", "date", "name", "value"]) would be more explicit. So the doc is not wrong, it's just a taste question at the end, and I'll say it's OK as is. Hope it helps, and thanks for your time reporting this, feedback on the doc are always appreciated! Bests, -- Julien Palard https://mdk.fr

Hi Julien, Thanks for elaborating on this. I got an error from the kw syntax right before I emailed but it must have been mismatched parentheses since I am not getting the error today on same machine and version. /b -----Original Message----- From: Julien Palard <julien@palard.fr> Sent: Saturday, October 16, 2021 10:41 AM To: Bill Melvin <Bill.Melvin@esc.edu> Cc: docs@python.org Subject: Re: [docs] Usage Example for csv.DictWriter Hi Bill! In the following syntax: class csv.DictWriter(f, fieldnames, restval='', extrasaction='raise', dialect='excel', *args, **kwds) I bet what make you think f and fieldnames are by position and not by name may be the fact that they hold no default values. But this is in fact not directly related. This is true that the set of arguments holding default values are optional, and this is true that to avoid listing all optional values when calling something there's a strong and good habit of giving optional arguments by name. But it's just a good habit, there's nothing enforced by the syntax (it can be enforced by using / and * but they are not used here). So all of the following are valid calls: DictWriter(f=csvfile, fieldnames=fieldnames) DictWriter(csvfile, fieldnames) DictWriter(fieldnames=fieldnames, f=csvfile) DictWriter(csvfile, fieldnames=fieldnames) I think the first one is too verbose while not being helpfull: `f` don't give any relevant information. The 3rd one is no better, inversing the order of argument is just misleading here. So the 2nd and the last one are valid, and only the "good taste" of a developer can tell which one is the more readable. In the case the field names are stored in a `fieldnames` variable, maybe `fieldnames=fieldnames` is a bit redundent and the 2nd one is the best. But in the case the field names are given literally, maybe DictWriter(csvfile, fieldnames=["id", "date", "name", "value"]) would be more explicit. So the doc is not wrong, it's just a taste question at the end, and I'll say it's OK as is. Hope it helps, and thanks for your time reporting this, feedback on the doc are always appreciated! Bests, -- Julien Palard https://mdk.fr
participants (2)
-
Bill Melvin
-
Julien Palard