On this page
  1. weakref.WeakMethod
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

weakref.WeakMethod

Prev: weakref.WeakKeyDictionary | Up: weakref | Next: weakref.WeakSet
Rel: weakref.CallableProxyType, weakref.finalize, weakref.getweakrefcount, weakref.getweakrefs
Ref: docs.python.org — weakref.WeakMethod
Tags: #public #python3 #stdlib #weakref


Import

import weakref
from weakref import WeakMethod

Signature & Overview

weakref.WeakMethod(meth, callback=None)

A custom weakref.ref subclass which simulates a weak reference to a bound method, working around the lifetime problem of bound methods.

Usage

import weakref

class Greeter:
    def hi(self): return 'hello'

g = Greeter()
wm = weakref.WeakMethod(g.hi)
assert wm()() == 'hello'
del g
assert wm() is None

Gotchas

REPL Inspect

from weakref import WeakMethod
help(WeakMethod)

Linked from

Related notes