Is this unpythonic?
Frank Millman
frank at chagford.com
Fri May 8 09:10:52 EDT 2015
"Ben Finney" <ben+python at benfinney.id.au> wrote in message
news:85y4kzb90f.fsf at benfinney.id.au...
> "Frank Millman" <frank at chagford.com> writes:
>
>> If I use a list as an argument, I only use it to pass values *into*
>> the function, but I never modify the list.
>
> You've had good reasons why a mutable default argument is a good idea.
>
> I'll address another aspect of the question: passing a structured
> collection of values via a single parameter.
>
> If those values *always* go together, it sounds like you have a class
> which should be defined to make it explicit that they go together.
>
[...]
>
> Too much overhead? If you want to collect a meaningful collection of
> values together in the same way each time, but don't need any special
> behaviour, then a class might be overkill.
>
> Python has the 'namedtuple' type::
>
[...]
>
> <URL:https://docs.python.org/3/library/collections.html#collections.namedtuple>
> documents the surprisingly useful 'namedtuple' factory.
>
Useful thoughts - thanks, Ben.
Here is a typical use case.
I have a function which constructs a sql SELECT statement from various input
parameters. One of the parameters is used to build up an ORDER BY clause.
If ordering is required, the caller passes a list of tuples, each tuple
consisting of a column name and a boolean indicating ascending/descending.
If no ordering is required, it is convenient for the caller to omit the
argument altogether, but then the callee needs a default argument.
Previously I had 'order=[]' as a keyword argument, but that gave rise to my
query. I have now changed it to 'order=None'.
Frank
More information about the Python-list
mailing list