6.7. Memory Management

Python uses a memory management scheme known as reference counting. For each object in the system, Python keeps track of how many variables reference that object. When the number of references to an object shrinks to zero, the memory used by the object is reclaimed or garbage collected.

The fact that variables are nothing more than references to objects is a very important one. Consider the following interactive Python session.

CD-ROM reference=6028.txt
>>> x = [1,2,3]
>>> print x
[1,2,3]
>>> y = x
>>> print y
[1, 2, 3]

The point here is that although both x and y will print the list [1,2,3], there is only one list. The variables x and y reference the same object. A diagram will help illustrate what is going on here. ...

Get XML Processing with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.