Zope Proxy

7.1 · active · verified Tue Apr 14

zope.proxy is a Python library providing generic, transparent proxies. It allows you to wrap an object with a proxy that delegates most operations to the underlying object, while still allowing for custom behavior or interception. The current version is 7.1, with releases typically tied to Python version support and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a transparent proxy for an object using `zope.proxy.Proxy`, interact with it, and retrieve the original object using `zope.proxy.getProxiedObject`. It also shows how `isinstance` works transparently.

from zope.proxy import Proxy, getProxiedObject

class MyObject:
    def __init__(self, value):
        self.value = value

    def get_value(self):
        return self.value

    def __repr__(self):
        return f"<MyObject value={self.value}>"

# Create an instance of MyObject
obj = MyObject(42)
print(f"Original object: {obj}")

# Wrap it with a Proxy
proxied_obj = Proxy(obj)
print(f"Proxied object: {proxied_obj}")

# Interact with the proxy - it behaves like the original object
print(f"Value via proxy: {proxied_obj.value}")
print(f"Method call via proxy: {proxied_obj.get_value()}")

# Check if it's a proxy
from zope.proxy import isproxy
print(f"Is proxied_obj a proxy? {isproxy(proxied_obj)}")

# Get the original object back from the proxy
original_from_proxy = getProxiedObject(proxied_obj)
print(f"Original object retrieved from proxy: {original_from_proxy}")
assert original_from_proxy is obj

# Demonstrate transparency for isinstance
print(f"isinstance(proxied_obj, MyObject)? {isinstance(proxied_obj, MyObject)}")

view raw JSON →