Late-binding of function defaults (was Re: What is a function parameter =[] for?)

Antoon Pardon antoon.pardon at rece.vub.ac.be
Wed Nov 25 03:14:48 EST 2015


Op 20-11-15 om 01:33 schreef Steven D'Aprano:
> On Fri, 20 Nov 2015 07:57 am, Marko Rauhamaa wrote:
>
>> Laura Creighton <lac at openend.se>:
>>
>>> My experience says that the people who are confused want lists to
>>> behave like tuples. period. i.e. they don't want lists to be mutable.
>> I think it's simpler than that. When you have:
>>
>>    def f(x=[]):
>>        y = []
>>
>> the first [] is evaluated when "def" is executed, while the latter [] is
>> evaluated whenever "f" is executed. It's easy to be confused.
> It shouldn't be. The function declaration 
>
>     def f(x=[]):
>
> is executed only once. The function body, conveniently indented to make it
> stand out:
>
>         y = []
>
> is executed every time you call the function. 

What exactly is your point? People's confusions don't disappear
because you as an expert have a good understanding of what is
going on and so are no longer confused.

Some aspects in the langauage are easily grasped and other
aspects tend to create confusion. I see nothing wrong with
people trying to point out what the cause of this confusion
could be. You arguing that people shouldn't be confused is
not helping.

-- 
Antoon.




More information about the Python-list mailing list