What is a function parameter =[] for?

Steven D'Aprano steve at pearwood.info
Tue Nov 24 13:33:23 EST 2015


On Wed, 25 Nov 2015 05:13 am, Marko Rauhamaa wrote:

> Personally, I don't like the "official" Python usage:
> 
> Objects whose value can change are said to be mutable
> 
> 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.

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.

Likewise for lists:


py> a = []
py> for i in range(100):
...     a.append(1)
...
py> b = [1]*100
py> a == b
True
py> sys.getsizeof(a) == sys.getsizeof(b)
False



-- 
Steven




More information about the Python-list mailing list