What is a function parameter =[] for?

Chris Angelico rosuav at gmail.com
Wed Nov 18 21:15:29 EST 2015


On Thu, Nov 19, 2015 at 1:08 PM, Ben Finney <ben+python at benfinney.id.au> wrote:
>> Nope. Mutable objects are never guaranteed to retain the same values.
>> That's kinda the point.
>
> Well, they are the *same value*, if by “value” you mean “a particular
> object”.
>
> On the other hand, the value can *change* over time, while remaining the
> same object. So it will not be equal to its initial state, even though
> it is the same object.

I would normally interpret "value" as "the quality compared with the
== operator". As such, mutables can change in value while retaining
their identities:

>>> x, y = [], []
>>> x == y
True
>>> x.append(1)
>>> x == y
False
>>> y.append(1)
>>> x == y
True

So a mutable default argument will always retain its identity (barring
shenanigans), but may not retain its value.

ChrisA



More information about the Python-list mailing list