Skip to main content
`Ctrl`+`K`

# `weakref`

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

ref(object, callback=None)

  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)

  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)

  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()

    True if the WeakMethod is still alive

class WeakMethodProxy(function, callback=None)

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

class WeakList(enumerator=())

  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)

    Add an object at the end of the list

  count(obj)

    Returns the number of occurencies of an object

  extend(other)

    Append the list with all objects from another list

  index(obj)

    Returns the index of an object

  insert(index, obj)

    Insert an object at the requested index

  pop(index=-1)

    Remove and return an object at the requested index

  remove(obj)

    Remove an object from the list

  reverse()

    Reverse the list inplace

  sort(key=None, reverse=False)

    Sort the list inplace. Not very efficient.