What is a function parameter =[] for?

Steven D'Aprano steve at pearwood.info
Tue Nov 24 11:11:28 EST 2015


On Tue, 24 Nov 2015 11:57 pm, Marko Rauhamaa wrote:

> Antoon Pardon <antoon.pardon at rece.vub.ac.be>:
> 
>> You then switching to talking about objects, just gives the impression
>> that object is a synonym for value.
> 
> It isn't?

No it isn't.

The definition of "object" -- well, I say "the" definition, but the nasty
truth is that there are many different definitions and nobody really agrees
on what an object is -- the definition of "object" is a software entity
with three fundamental attributes or qualities:

- the type of object;

- the value of the object;

- the identity of the object.

If we limit ourselves to Python, that definition works because Python has
been designed to work that way. Other languages may be different, but when
it comes to Python, it is true by definition that all objects have a type,
a value and an identity because that's what Guido did.

The type of the object is the class that it is an instance of:

py> type("hello")
<class 'str'>

The identity of the object is represented by the integer ID returned by the
id() function. But what it represents is the distinct existence of one
object as compared to another. In real life, the identity of *this* sheep
versus *that* sheep, or this instance of a book versus that instance of the
same book.

(Aside: those of a philosophical bent will quickly realise that we don't
really understand what identity *is*. See, for example, the paradox of my
gradfather's axe. And what distinguishes this electron from some other
electron? There's even a theory in physics that *all electrons are in fact
the one electron*, bouncing backwards and forwards in time repeatedly.)

The value of the object depends on its type -- the value of ints differ from
the value of strings, for example.

Immutable objects have a fixed value for their entire lifespan: the object 2
has the same numeric value, namely two, from the moment it is instantiated
to the moment it is garbage collected.

Mutable objects do not have such a fixed value:

py> L = []  # value is the empty list
py> L.append(None)  # value is now a singleton list containing None
py> L[:] = 'cat dog fox'.split()  # value is now a list containing strings
cat, dog, fox.
py> L
['cat', 'dog', 'fox']


It's the same list, the same object, but the value changes.




-- 
Steven




More information about the Python-list mailing list