What is a function parameter =[] for?
BartC
bc at freeuk.com
Fri Nov 20 07:53:00 EST 2015
On 20/11/2015 12:28, Marko Rauhamaa wrote:
> BartC <bc at freeuk.com>:
>> We're arguing at cross-purposes then since you are obviously
>> interested in these esoteric aspects,
>
> The Ackermann function really is an esoteric example, but the other
> example that has been discussed here can make practical use of the
> default-value semantics:
And you go on to give a couple of slightly less esoteric examples! (I
don't think I've ever written a lambda function in my life.)
> Even more appropriately, you may expressly want a mutable, singleton
> object to be the default:
>
> def initiate_query(query, database=global_database):
Finally, a down-to-earth example. Here it probably doesn't matter at
what point 'global_database' gets bound. You know it will always refer
to the current state of global_database, and you know that it is a data
structure external to the function even if it is updated from inside.
That is a bit different from an empty list that later is not empty.
> def asklist(caption, data, n=omitted, rows=omitted, width=omitted,
> flags=omitted, buttons=omitted, tablist=omitted,
> heading=omitted):
>
> but that would be rather pedantic in most circumstances.
Yes, that's along the lines of what I used do before I had proper
default values!
Then I had code inside the function to test a parameter and set it as
needed. I still do this when the default isn't straightforward (where it
depends on other parameters for example).
--
Bartc
More information about the Python-list
mailing list