Skip to main content

A Descriptor class implementing weakref properties in Python 2/3

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

Metadata

Release files for weakref_property 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for weakref_property 0.1
File Size Uploaded Provenance Actions
weakref_property-0.1.tar.gz 14.4 kB Without provenance Details for weakref_property-0.1.tar.gz

Release files / weakref_property-0.1.tar.gz

Download URL weakref_property-0.1.tar.gz
Size 14.4 kB
Tags Source
SHA-256 checksum
How to use checksums
258180f09fc0eb19a4483b642796c9000a31e0f25491635cf4e91850b91ea32d
BLAKE2b-256 checksum
How to use checksums
d032fe976e59a132a52ff79e3a714b8f938a518205ddf490634dfac39aed2194
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for weakref_property 0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page