What is a function parameter =[] for?
Antoon Pardon
antoon.pardon at rece.vub.ac.be
Tue Nov 24 08:43:33 EST 2015
Op 20-11-15 om 01:27 schreef Ned Batchelder:
> On Thursday, November 19, 2015 at 7:11:52 PM UTC-5, BartC wrote:
>> On 19/11/2015 22:55, Michael Torrie wrote:
>>> On 11/19/2015 02:21 PM, BartC wrote:
>>>> (Python returns 42; so that means my languages are more dynamic than
>>>> Python? That's hard to believe!)
>>>
>>> It tells me your language does late binding for default arguments, which
>>> does mean the default argument can dynamically change at call time,
>>> which would surprise me if I didn't know about it. Either form of
>>> binding is acceptable, and I don't think it makes a language more or
>>> less dynamic.
>>
>> You get the expression that is specified, which can give different
>> values at different times unless it involves only constants.
>>
>> It can't be exactly the same as writing an identical expression in place
>> of the missing argument, as apparently different scopes come into play
>> if names are involved.
>>
>> However I mainly use them for constant values. And [] is a constant
>> value in my opinion.
>>
>> --
>> Bartc
>
> You are not alone in being surprised by how mutable default values work.
> It is a topic that comes up in every "What's Bad About Python" discussion,
> and is a common question from new users of the language.
>
> I can understand how you would view [] as a constant value. It's true that
> it is an expression that produces a consistent value each time it is
> evaluated. But that value is a list, and lists are mutable. The examples
> here all use the .append() method on that value, which changes it.
I think that part of the problem is, that [] is not a constant object. So
that when you see a line like
ls = []
It behaves more lke
ls = [].copy()
than what you would expect with the normal python semantics.
--
Antoon
More information about the Python-list
mailing list