Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

A Descriptor class implementing weakref properties in Python

Project Description

Add a property that acts like a normal attribute, but keeps a weak reference to anything that is assigned to it.

Example:

First let’s define WeakValue using WeakProperty and SomeClass that will be weakly referenced:

>>> import weakref_property
>>> import gc

>>> class WeakValue(object):
...     value = weakref_property.WeakProperty('value')

>>> class SomeClass(object):
...     pass

You can assign and retrieve a value:

>>> a = SomeClass()
>>> obj = WeakValue()
>>> obj.value = a
>>> obj.value  # doctest: +ELLIPSIS
<__main__.SomeClass object at ...>

But it is kept as a weakref, so it can get collected. If it is collected, the property returns None:

>>> del a
>>> gc.collect()  # force gc to collect `a` object
0
>>> obj.value

You can also delete the value completelly:

>>> del obj.value
>>> obj.value
Traceback (most recent call last):
    ...
AttributeError: 'WeakValue' object has no attribute 'value'

Sadly, weakrefs do not work on tuples, ints and not-subclassed lists and dicts:

>>> obj.value = []
Traceback (most recent call last):
    ...
TypeError: cannot create weak reference to 'list' object
Release History

Release History

This version
History Node

0.1

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
weakref_property-0.1.tar.gz (14.4 kB) Copy SHA256 Checksum SHA256 Source Aug 16, 2014

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting