On 5/13/18 4:02 PM, Mike McClain wrote:
> I'm new to Python and OOP.
> Python  en 2.7.14 Documentation  The Python Language Reference
> 3. Data model
> 3.1. Objects, values and types
> 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.
>
> It appears to me as if an object's type is totally mutable and
> solely dependant on assignment.
>
>>>> obj = 'a1b2'
>>>> obj
> 'a1b2'
>>>> type (obj)
> <type 'str'>
>>>> obj = list(obj)
>>>> obj
> ['a', '1', 'b', '2']
>>>> type (obj)
> <type 'list'>
>>>> obj = dict( zip(obj[0::2],obj[1::2]) )
>>>> type (obj)
> <type 'dict'>
>>>> obj
> {'a': '1', 'b': '2'}
>
> At what level does my understanding break down?
>
> Thanks,
> Mike
> --
> One reason experience is such a good teacher
>     is that she doesn't allow dropouts.

The first this is obj is NOT 'the object', but is instead a reference
that 'points' to an object.

When you said

obj = 'a1b2'

a string object with the value a1b2 was created, and obj was pointed to it.

when you then said

obj = list(obj)

then a new object, a list, was created based on the value of the object
that obj was pointing to (the string 'a1b2') and then obj was set to
point to that new object, and the old string object became unreferenced
and will at some point be cleaned up (if between the two assignment you
did a obj2 = obj, then obj2 would continue to point to that original
string object).

Note that this says that after you assigned obj to the dict, if you then
assigned obj2 = obj and then did an operation that changed the value
stored in the dict (like using a mutating member function that adds a
member to the dict), obj2 would see the changes, because they both refer
to the same fundamental object.

-- 
Richard Damon

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to