object types, mutable or not?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon May 14 08:02:49 EDT 2018


On Sun, 13 May 2018 13:02:01 -0700, Mike McClain wrote:

[...]
> It appears to me as if an object's type is totally mutable and solely
> dependant on assignment.
> 
>>>> obj = 'a1b2'
>>>> type (obj)
> <type 'str'>
>>>>
>>>> obj = list(obj)
>>>> type (obj)
> <type 'list'>



> At what level does my understanding break down?

Mistaking a name (which refers to an object) for the object itself.

Five years ago, the President Of the United States of America, or POTUS 
for short, referred to Barrack Obama. Today, it refers to Donald Trump. 
This didn't happen by mutating a single person (an object) from a 
youngish black-skinned man to an oldish orange-skinned man. It happened 
by re-assigning the name from Obama to Trump.

Likewise when you re-assign a variable name from one value (an object) to 
another, the original object doesn't change. You just make the name refer 
to a different object, while the first goes on its merry way (probably to 
be collected by the garbage collector and the memory reclaimed).


-- 
Steve




More information about the Python-list mailing list