HappyDoc Generated Documentation | HappyDoc3-r3_1/happydoclib/docstring/StructuredText/regressions/Acquisition.stx |
---|---|
/ HappyDoc3-r3_1 / happydoclib / docstring / StructuredText / regressions / Acquisition.stx AcquisitionCopyright (C) 1996-1998, Digital Creations. Acquisition [1] is a mechanism that allows objects to obtain attributes from their environment. It is similar to inheritence, except that, rather than traversing an inheritence hierarchy to obtain attributes, a containment hierarchy is traversed. The ExtensionClass. release includes mix-in extension base classes that can be used to add acquisition as a feature to extension subclasses. These mix-in classes use the context-wrapping feature of ExtensionClasses to implement acquisition. Consider the following example: import ExtensionClass, Acquisition class C(ExtensionClass.Base): color='red' class A(Acquisition.Implicit): def report(self): print self.color a=A() c=C() c.a=A() c.a.report() # prints 'red' d=C() d.color='green' d.a=a d.a.report() # prints 'green' a.report() # raises an attribute error The class Acquisition wrappers When an object that supports acquisition is accessed through
an extension class instance, a special object, called an
acquisition wrapper, is returned. In the example above, the
expression Aquisition wrappers provide access to the wrapped objects
through the attributes 'c.a.aq_parent is c' and: 'c.a.aq_self is a' both evaluate to true, but the expression: 'c.a is a' evaluates to false, because the expression The attribute Acquisition ControlTwo styles of acquisition are supported in the current ExtensionClass release, implicit and explicit aquisition. Implicit acquisitionImplicit acquisition is so named because it searches for attributes from the environment automatically whenever an attribute cannot be obtained directly from an object or through inheritence. An attribute may be implicitly acquired if it's name does
not begin with an underscore, To support implicit acquisition, an object should inherit
from the mix-in class Explicit Acquisition When explicit acquisition is used, attributes are not
automatically obtained from the environment. Instead, the
method print c.a.aq_acquire('color') To support explicit acquisition, an object should inherit
from the mix-in class Controlled AcquisitionA class (or instance) can provide attribute by attribute control over acquisition. This is done by:
Filtered Acquisition The acquisition method, The filter function is called with five arguments:
If the filter returns a true object that the object found is returned, otherwise, the acquisition search continues. For example, in: from Acquisition import Explicit class HandyForTesting: def __init__(self, name): self.name=name def __str__(self): return "%s(%s)" % (self.name, self.__class__.__name__) __repr__=__str__ class E(Explicit, HandyForTesting): pass class Nice(HandyForTesting): isNice=1 def __str__(self): return HandyForTesting.__str__(self)+' and I am nice!' __repr__=__str__ a=E('a') a.b=E('b') a.b.c=E('c') a.p=Nice('spam') a.b.p=E('p') def find_nice(self, ancestor, name, object, extra): return hasattr(object,'isNice') and object.isNice print a.b.c.aq_acquire('p', find_nice) The filtered acquisition in the last line skips over the first
attribute it finds with the name spam(Nice) and I am nice! Acquisition and methods Python methods of objects that support acquisition can use
acquired attributes as in the Unfortunately, C methods defined in extension base classes that define their own data structures, cannot use aquired attributes at this time. This is because wrapper objects do not conform to the data structures expected by these methods. Acquiring Acquiring objectsConsider the following example: from Acquisition import Implicit class C(Implicit): def __init__(self, name): self.name=name def __str__(self): return "%s(%s)" % (self.name, self.__class__.__name__) __repr__=__str__ a=C("a") a.b=C("b") a.b.pref="spam" a.b.c=C("c") a.b.c.color="red" a.b.c.pref="eggs" a.x=C("x") o=a.b.c.x The expression In the current release of ExtensionClass, the expression "o.color"
does indeed return When searching for an attribute in If desired, the current rules for looking up attributes in complex
expressions can best be understood through repeated application of
the
and by keeping in mind that attribute lookup in a wrapper
is done by trying to lookup the attribute in the wrapped object
first and then in the parent object. In the expressions above
involving the Note that heuristics are used to avoid most of the repeated
lookups. For example, in the expression: [1] Gil, J., Lorenz, D., Environmental Acquisition--A New Inheritance-Like Abstraction Mechanism, OOPSLA '96 Proceedings, ACM SIG-PLAN, October, 1996 |