Python Programming, news on the Voidspace Python Projects and all things techie.
A Little Bit of Python: Episodes 9 and 10
We're on a roll with A Little Bit of Python, with two more episodes now up. A Little Bit of Python is an occasional podcast on Python related topics with myself, Brett Cannon, Jesse Noller, Steve Holden and Andrew Kuchling.
Episode 9 is a round-table discussion of various bits of news. We discuss some (more) of the new things coming in Python 2.7 (argparse plus improvements to the unittest and ElementTree modules), some news from other implementations of Python (specifically PyPy, IronPython and Jython), and some other bits and pieces of Python related news.
- A Little Bit of Python Episode List
- Episode 9 (mp3)
- Episode 9 (m4a)
- Episode 10 (mp3)
- Episode 10 (m4a)
This is the first episode put together where we recorded all the audio streams separately, so the audio quality is much better than previous episodes. It still isn't perfect but we're moving along the learning curve.
A Little Bit of Python has an iTunes page and a Twitter account, so you have no excuse for missing it:
If you have feedback, insults or suggestions for new topics you can email us on: firstname.lastname@example.org.
WeakSet: A new weak reference collection class
Weak references are a fairly standard language feature that allows you to keep references to objects without preventing them being garbage collected. In Python weak references are provided by the weakref module.
Not all objects are weak referenceable, but instances of user defined classes (plus subclasses of some of the built-in types like dict and list) are. Weak references are callable, and either return the original object or None if it has been garbage collected since the reference was created.
>>> from weakref import ref >>> class Foo(dict): pass ... >>> f = Foo() >>> weak_reference = ref(f) >>> f is weak_reference() True >>> del f >>> print weak_reference() None
Weak references are useful for tracking objects without leaking memory by keeping objects alive beyond their natural lifetime. The Ordered Dictionary (new in Python 2.7 and 3.1) very cleverly uses a doubly-linked list to maintain order but still provide efficient member deletion (O(1) rather than O(n)). The list is linked with weak references to avoid creating a cycle, which could then only be collected by the cyclic garbage collector rather than the standard Python deterministic reference counting. Raymond explains the ordered dictionary implementation in this excellent PyCon Presentation.
The weakref module includes several container classes like the WeakKeyDictionary and WeakValueDictionary for managing collections of weak references. These collections abstract away the weak references themselves so you only work with the container classes and your objects. As objects go away they are automatically removed from the collection.
New in Python 3.1 is the WeakSet which was actually implemented to manage class registration in the Abstract Base Class machinery. I've now backported this to Python 2.7, as it is a generally useful class, and put it up on PyPI for Python 2.6 and 2.5. Many thanks to Thomas Wouters for the original implementation and the very good set of tests that made backporting it trivially easy.
You can install it with: pip install weakrefset
Usage is straightforward. You don't need to deal with weak references themselves, the WeakSet manages the references and you can add / remove / iterate over your objects directly:
>>> from weakrefset import WeakSet >>> class Foo(object): pass ... >>> f1 = Foo() >>> f2 = Foo() >>> refs = WeakSet([f1, f2]) >>> f3 = Foo() >>> refs.add(f3) >>> refs.remove(f1) >>> del f2 >>> f3 in refs True >>> for f in refs: ... print f ... <__main__.Foo object at 0x40e410>
To be compatible with Python 2.5, 2.6 and future versions of Python you can import it like this:
try: # location in Python 2.7 and 3.1 from weakref import WeakSet except ImportError: # separately installed from weakrefset import WeakSet
This work is licensed under a Creative Commons Attribution-Share Alike 2.0 License.