|
Original |
Translation |
|
67
|
>>> 'andrew'.capitalize() 'Andrew' >>> 'hostname'.replace('os', 'linux') 'hlinuxtname' >>> 'moshe'.find('sh') 2
|
|
|
68
|
One thing that hasn't changed, a noteworthy April Fools' joke notwithstanding, is that Python strings are immutable. Thus, the string methods return new strings, and do not modify the string on which they operate.
|
|
|
69
|
The old :mod:`string` module is still around for backwards compatibility, but it mostly acts as a front-end to the new string methods.
|
|
|
70
|
Two methods which have no parallel in pre-2.0 versions, although they did exist in JPython for quite some time, are :meth:`startswith` and :meth:`endswith`. ``s.startswith(t)`` is equivalent to ``s[:len(t)] == t``, while ``s.endswith(t)`` is equivalent to ``s[-len(t):] == t``.
|
|
|
71
|
|
72
|
Garbage Collection of Cycles
|
|
|
73
|
The C implementation of Python uses reference counting to implement garbage collection. Every Python object maintains a count of the number of references pointing to itself, and adjusts the count as references are created or destroyed. Once the reference count reaches zero, the object is no longer accessible, since you need to have a reference to an object to access it, and if the count is zero, no references exist any longer.
|
|
|
74
|
Reference counting has some pleasant properties: it's easy to understand and implement, and the resulting implementation is portable, fairly fast, and reacts well with other libraries that implement their own memory handling schemes. The major problem with reference counting is that it sometimes doesn't realise that objects are no longer accessible, resulting in a memory leak. This happens when there are cycles of references.
|
|
|
75
|
Consider the simplest possible cycle, a class instance which has a reference to itself::
|
|
|
76
|
instance = SomeClass() instance.myself = instance
|
|