What is a function parameter =[] for?

Antoon Pardon antoon.pardon at rece.vub.ac.be
Tue Nov 24 09:38:26 EST 2015


Op 20-11-15 om 14:04 schreef Chris Angelico:
> On Fri, Nov 20, 2015 at 11:39 PM, BartC <bc at freeuk.com> wrote:
>> * The refusal to acknowledge that the def fn(a=[]) syntax is misleading.
>> (What value will a have when you call fn()? The true answer is that you
>> can't tell.)
> 
> It isn't misleading. The default value for the argument is set when
> the function is defined, and it is set to the *object* that results
> from evaluating the *expression* given. After that, the *object* is
> the one you will always get (barring shenanigans) if the argument is
> omitted. If the value of that object changes, it changes!

That you can perfectly explain what happens, doesn't contradict that
it is misleading. On the contrary, the fact that one needs all this
explanation in order to grasp what is happening, confirms that it
is misleading.

And you may be talking about objects here. The tutorial talks about
values.

> You keep expecting the *value* to be consistent. But what you actually
> get is that the *object* is consistent. It's virtually impossible to
> guarantee the former, in the face of mutable objects.

What is wrong with expecting the value to be consistent when the tutorial
talks about values?

-- 
Antoon.




More information about the Python-list mailing list