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

Steven D'Aprano steve at pearwood.info
Wed Nov 25 19:53:51 EST 2015


On Thu, 26 Nov 2015 01:34 am, BartC wrote:

> On 25/11/2015 13:53, Marko Rauhamaa wrote:
>> BartC <bc at freeuk.com>:
>>
>>> Using tuples in the same way that other languages implement records is
>>> going to be difficult if you can't change the values of the fields!
>>
>> Guido could decide tomorrow that tuples are mutable.
> 
> (Could that be done without breaking existing code? And what then would
> be the difference between them and lists?)


No it could not, and no Guido could not decide to make them mutable. 

Well, strictly speaking he could make that decision, but the other core
developers would not go along with such a stupid backwards-incompatible
change. I don't know what would happen if Guido did make that decision -- I
suppose it would provoke some sort of constitutional crisis (figuratively
speaking), like what would happen if the Queen of the UK refused to sign
into law legislation after being lawfully asked to do so by the Prime
Minister. (The Queen has the power to reject any legislation put to her by
the Prime Minister, so long as she does not actually make use of that
power.)

Guido, as BDFL, has every right to make tuples mutable, or remove numbers
from the language, so long as he does not actually do so.

Making tuples mutable would break their use as dictionary keys, which is a
*critical* use.

https://docs.python.org/2/faq/design.html#why-must-dictionary-keys-be-immutable



>> Anyway, Python has two ways to represent records: classes and tuples.
>> Tuples are nice because they are concise and ad hoc. Often you start
>> with a scalar value, then turn it into a tuple. After a while your handy
>> tuple turns out a bit cumbersome to use so you convert it into an actual
>> class.
> 
> This is how records are handled in another language which I believe is
> simpler than Python: http://pastebin.com/vhsJML8U
> 
> This also implements my example from earlier in the thread. Now compare
> with Python's approach.
> 
> OK, that also needs two concepts, a list, and a record. But the
> distinction is now completely obvious. Records are also mutable. It
> would also be clear why you can't append to a record.
> 
> Now try explaining again to me how you would use tuples for the same
> thing...

In Pascal, strings are mutable. (Well, technically, standard Pascal doesn't
define a string data type, but most implementations do.) So you typically
have some function like:

insert(thestring, substring, pos)

which modifies thestring in place. In Python, strings are immutable, so the
equivalent of insert() would be a function that returns a new string:

thestring = insert(thestring, substring, pos)

It's not exactly an earth-shattering difference.

The implementation is simple that it is almost pointless to put it in a
function:

def insert(s, substring, pos):
    return s[:pos] + substring + s[pos:]


Tuples-as-records are no different. Instead of modifying the existing
record, you create a new one. If that's inconvenient, then use a class,
which will be mutable by default.



-- 
Steven




More information about the Python-list mailing list