What is a function parameter =[] for?

Marko Rauhamaa marko at pacujo.net
Tue Nov 24 14:17:10 EST 2015


Steven D'Aprano <steve at pearwood.info>:

> On Wed, 25 Nov 2015 05:13 am, Marko Rauhamaa wrote:
>> I would prefer this wording:
>> 
>> Objects whose inner state can change are said to be mutable
>
> I see your point, but "inner state" might not be related to the
> object's externally visible value.

The inner state is indeed inaccessible. We cannot determine immutability
by testing, but we *can* determine mutability (if we're lucky).

> For example, dicts have an inner state which can vary, even when their value
> remains the same:
>
> # using Python 3.3
>
> py> d = {-1: "a", -2: "b"}
> py> e = {-2: "b", -1: "a"}
> py> d == e
> True
> py> print(d, e)
> {-2: 'b', -1: 'a'} {-1: 'a', -2: 'b'}
>
> The two dicts have the same value, but their inner state is slightly
> different, which is why they print in different order.

I'd say an object o is mutable (at least) if it has got a method m such
that:

    before = o.m()
    # any intervening code that does not reassign o
    after = o.m()
    before == after
    => False


Marko



More information about the Python-list mailing list