weakref

Weakref utils for compatibility between Python 2 and Python 3 or for extended features.

ref(object, callback=None)[source]

Returns a weak reference to object. The original object can be retrieved by calling the reference object if the referent is still alive. If the referent is no longer alive, calling the reference object will cause None to be returned.

The signature is the same as the standard weakref library, but it returns WeakMethod if the object is a bound method.

Parameters:
  • object – An object
  • callback (func) – If provided, and the returned weakref object is still alive, the callback will be called when the object is about to be finalized. The weak reference object will be passed as the only parameter to the callback. Then the referent will no longer be available.
Returns:

A weak reference to the object

proxy(object, callback=None)[source]

Return a proxy to object which uses a weak reference. This supports use of the proxy in most contexts instead of requiring the explicit dereferencing used with weak reference objects.

The signature is the same as the standard weakref library, but it returns WeakMethodProxy if the object is a bound method.

Parameters:
  • object – An object
  • callback (func) – If provided, and the returned weakref object is still alive, the callback will be called when the object is about to be finalized. The weak reference object will be passed as the only parameter to the callback. Then the referent will no longer be available.
Returns:

A proxy to a weak reference of the object

class WeakMethod(function, callback=None)[source]

Wraps a callable object like a function or a bound method. Feature callback when the object is about to be finalized. Provids the same interface as a normal weak reference.

is_alive()[source]

True if the WeakMethod is still alive

class WeakMethodProxy(function, callback=None)[source]

Wraps a callable object like a function or a bound method with a weakref proxy.

class WeakList(enumerator=())[source]

Manage a list of weaked references. When an object is dead, the list is flaged as invalid. If expected the list is cleaned up to remove dead objects.

append(obj)[source]

Add an object at the end of the list

count(obj)[source]

Returns the number of occurencies of an object

extend(other)[source]

Append the list with all objects from another list

index(obj)[source]

Returns the index of an object

insert(index, obj)[source]

Insert an object at the requested index

pop(index=-1)[source]

Remove and return an object at the requested index

remove(obj)[source]

Remove an object from the list

reverse()[source]

Reverse the list inplace

sort(key=None, reverse=False)[source]

Sort the list inplace. Not very efficient.