Proxy-related interfaces.
Provides methods for indentifying proxies and extracting proxied objects
Check whether the given object is a proxy
If proxytype is not None, checkes whether the object is proxied by the given proxytype.
Check whether ob1 and ob2 are the same or proxies of the same object
Get the proxied Object
If the object isn’t proxied, then just return the object.
Set the underlying object for ob1 to ob2, returning the old object.
Raises TypeError if ob1 is not a proxy.
Get the proxied object with no proxies
If obj is not a proxied object, return obj.
The returned object has no proxies.
More convenience functions for dealing with proxies.
getProxiedObject(proxy) –> object
Get the underlying object for proxy, or the object itself, if it is not a proxy.
setProxiedObject(proxy, object) –> object
Set the underlying object for proxy, returning the old proxied object. Raises TypeError if proxy is not a proxy.
removeAllProxies(proxy) –> object
Get the proxied object with no proxies
If obj is not a proxied object, return obj.
The returned object has no proxies.
Check whether two objects are the same or proxies of the same object
Look for a proxy of the given type around the object
If no such proxy can be found, return the default.
Check whether the given object is a proxy
If proxytype is not None, checkes whether the object is proxied by the given proxytype.
Look for the inner-most proxy of the given type around the object
If no such proxy can be found, return the default.
If there is such a proxy, return the inner-most one.
Decorator support
Decorators are proxies that are mostly transparent but that may provide additional features.
>>> from zope.interface import Interface
>>> from zope.interface import directlyProvides
>>> from zope.interface import implementer
>>> class I1(Interface):
... pass
>>> class I2(Interface):
... pass
>>> class I3(Interface):
... pass
>>> class I4(Interface):
... pass
>>> from zope.proxy.decorator import SpecificationDecoratorBase
>>> @implementer(I1)
... class D1(SpecificationDecoratorBase):
... pass
>>> @implementer(I2)
... class D2(SpecificationDecoratorBase):
... pass
>>> @implementer(I3)
... class X(object):
... pass
>>> x = X()
>>> directlyProvides(x, I4)
Interfaces of X are ordered with the directly-provided interfaces first.
>>> from zope.interface import providedBy
>>> [interface.getName() for interface in list(providedBy(x))]
['I4', 'I3']
When we decorate objects, what order should the interfaces come in? One could argue that decorators are less specific, so they should come last.
>>> [interface.getName() for interface in list(providedBy(D1(x)))]
['I4', 'I3', 'I1']
>>> [interface.getName() for interface in list(providedBy(D2(D1(x))))]
['I4', 'I3', 'I1', 'I2']
SpecificationDecorators also work with old-style classes:
>>> @implementer(I3)
... class X:
... pass
>>> x = X()
>>> directlyProvides(x, I4)
>>> [interface.getName() for interface in list(providedBy(x))]
['I4', 'I3']
>>> [interface.getName() for interface in list(providedBy(D1(x)))]
['I4', 'I3', 'I1']
>>> [interface.getName() for interface in list(providedBy(D2(D1(x))))]
['I4', 'I3', 'I1', 'I2']