Python Glossary Русский

all Python 3weakrefterm

weakref.CallableProxyType

weak references

The type of a proxy to a callable object — a function or an object with __call__. The proxy itself can be called too.

Syntax

type(p) is weakref.CallableProxyType

weakref.proxy picks the type from the object: if it has __call__, the proxy becomes a CallableProxyType and p() is forwarded. The trap is a proxy to a bound method: obj.method is created anew on every access and dies at once, so such a proxy is dead from the first line. WeakMethod exists for methods.

Examples

import weakref
class Counter:
    def __call__(self):
        return 1
c = Counter()
p = weakref.proxy(c)
print(type(p) is weakref.CallableProxyType, p())  # → True 1

See also

Open in the full glossarydocs.python.orgReport an inaccuracy