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

Laura Creighton lac at openend.se
Thu Nov 19 15:18:01 EST 2015


In a message of Fri, 20 Nov 2015 06:46:42 +1100, Chris Angelico writes:

>Would this satisfy the people who get confused about "=[]"?
>
>ChrisA

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.  Which means when I say 'use a tuple instead' they go
away happy. Lots of very occasional programmers in my world (children)
just use tuples instead of lists and never get surprises and 
never go on to learn about lists and why they are a good idea.

But then there are those who come back with the problem:
'my python program is too slow'.  From a perspective of
'Let us understand why this is so and what could we do to make
things faster', I can get them to come up with the idea of a 
python list all on their own.  But this requires the sort of
brain that is interested in how interpreters work, otherwise
this conversation will not happen.

Without the actual problem biting them, the whole idea of
mutable objects seems, ah, hazardous.  I wonder if we do people
a disservice by introducing them straight off in teaching python
to absolute beginners, and if the learning would go easier if
we taught tuples and made them all use them a while before we
gave them lists.

At any rate, 'what is a mutable object' is a conversation that I 
have had too many times with people who really aren't interested.
I now think that 'use a tuple instead' is the way to go, with the
addition -- come back and talk to me about this at any time if you
want to learn why this is so.

Laura




More information about the Python-list mailing list