Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #90166

Re: Is this unpythonic?

From "Frank Millman" <frank@chagford.com>
Subject Re: Is this unpythonic?
Date 2015-05-08 15:10 +0200
References <mihqhb$r1k$1@ger.gmane.org> <85y4kzb90f.fsf@benfinney.id.au>
Newsgroups comp.lang.python
Message-ID <mailman.248.1431090654.12865.python-list@python.org> (permalink)

Show all headers | View raw


"Ben Finney" <ben+python@benfinney.id.au> wrote in message 
news:85y4kzb90f.fsf@benfinney.id.au...
> "Frank Millman" <frank@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


Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Is this unpythonic? "Frank Millman" <frank@chagford.com> - 2015-05-08 15:10 +0200

csiph-web