On 2018-05-13 13:02:01 -0700, Mike McClain wrote: > I'm new to Python and OOP. > Python en 2.7.14 Documentation The Python Language Reference
Python 2 is near end-of-life. If you are new to Python you should use Python 3 (unless you have to maintain legacy software written in Python 2). > 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? At the point where you assume that the variable “obj” always contains the same objects. This is not the case. In fact, variables in Python never “contain” anything, they are only “bound to” objects (or in other words, they refer to, or point to objects). > >>> obj = 'a1b2' Here an object of type “str” and value 'a1b2' is created. > >>> obj > 'a1b2' > >>> type (obj) > <type 'str'> > >>> > >>> obj = list(obj) Here a new object of type list is created. The variable “obj” is bound to this new object. The old str object isn't needed anymore and destroyed. > >>> obj > ['a', '1', 'b', '2'] > >>> type (obj) > <type 'list'> > >>> > >>> obj = dict( zip(obj[0::2],obj[1::2]) ) And here you create another new object (this time a dict) and bind the variable to this new obj. Again, the old object isn't needed any more and destroyed. There is a good talk about this topic by Ned Batchelder on Youtube: https://www.youtube.com/embed/_AEJHKGk9ns 25 minutes well spent. hp -- _ | Peter J. Holzer | we build much bigger, better disasters now |_|_) | | because we have much more sophisticated | | | h...@hjp.at | management tools. __/ | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/>
signature.asc
Description: PGP signature
-- https://mail.python.org/mailman/listinfo/python-list