object types, mutable or not?

Ben Finney ben+python at benfinney.id.au
Sun May 13 19:37:22 EDT 2018


Mike McClain <mike.junk.46 at att.net> writes:

> I'm new to Python and OOP.

Welcome! Congratulations on learning Python.

> Python  en 2.7.14 Documentation

I will concur with others in this thread: Learning Python today you
should learn Python 3. Avoid Python 2 for now (you can learn its
obsolete quirks later).

> An object's type is also unchangeable. [1]
> [1]     It is possible in some cases to change an object's type,
>     under certain controlled conditions.

Yes.

> It appears to me as if an object's type is totally mutable and
> solely dependant on assignment.

You are mistaking re-binding of a name, for changing the object. They
are not the same thing.

Let's walk through the meaning of each of the statements in your example:

> >>> obj = 'a1b2'

This creates a new object, the string ‘'a1b2'’; then binds the name
‘obj’ to that object.

> >>> obj
> 'a1b2'

This interrogates what object the name ‘obj’ currently refers to.

Because this is done in the Python REPL, it also emits a text
representation of that object.

> >>> type (obj)
> <type 'str'>

This interrogates what object the name ‘obj’ currently refers to, then
asks for the type of that object. For the referenced object, that is the
‘str’ type.

Because this is done in the Python REPL, it also emits a text
representation of the ‘str’ type (the type is also an object, of type
‘type’).

> >>> obj = list(obj)

This interrogates what object the name ‘obj’ currently refers to. Then,
it invokes the ‘list’ type constructor to create a new instance.

Then, the name ‘obj’ is bound to the new list instance that was created.

> >>> obj
> ['a', '1', 'b', '2']

This interrogates what object the name ‘obj’ currently refers to.

Because this is done in the Python REPL, it also emits a text
representation of that object.

> >>> type (obj)
> <type 'list'>

This interrogates what object the name ‘obj’ currently refers to, then
asks for the type of that object. For the referenced object, that is the
‘list’ type.

Because this is done in the Python REPL, it also emits a text
representation of the ‘list’ type (the type is also an object, of type
‘type’).

> >>> obj = dict( zip(obj[0::2],obj[1::2]) )

This interrogates what object the name ‘obj’ currently refers to, then
performs a pair of slicing operations on that object; those operations
return new instances each of type ‘list’.

Then, the ‘zip’ function is invoked with those list objects; the return
value is itself a new list.

Then, the ‘dict’ type constructor is invoked to create a new instance,
based on that list. It returns a new ‘dict’ instance.

Then, the name ‘obj’ is bound to that dict instance.

> >>> type (obj)
> <type 'dict'>

This interrogates what object the name ‘obj’ currently refers to, then
asks for the type of that object. For the referenced object, that is the
‘dict’ type.

Because this is done in the Python REPL, it also emits a text
representation of the ‘dict’ type (the type is also an object, of type
‘type’).

> >>> obj
> {'a': '1', 'b': '2'}

This interrogates what object the name ‘obj’ currently refers to.

Because this is done in the Python REPL, it also emits a text
representation of that object.

> At what level does my understanding break down?

Assignment in Python never changes the referenced object. It changes the
*reference*, to refer to some (usually a different) object. The
referenced object is not even invoked, and is certainly not changed, by
the assignment.

Your misunderstanding probably comes from other programming languages,
that have a concept of “variable” that resenbles a box of a particular
shape, that you put an object into.

Python does not have that concept. Assignment binds a name *to* an
object, like a sticky note or a paper tag. The binding doesn't affect
the object, and can change at some future time to be bound to a
different object without ever affecting the earlier object.

See this excellent talk by Ned Batchelder:

    Ned Batchelder: Facts and myths about Python names and values
    <URL:https://nedbatchelder.com/text/names.html>

-- 
 \          “In general my children refuse to eat anything that hasn't |
  `\                              danced on television.” —Erma Bombeck |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list