I have a class in my zope hierachy which implements an __getitem__ method and return objects which are created on the fly (or taken from a volatile (_v_) dictonary).
What do I have to do if I want those objects to take part in the acquisition machinery.
Sascha
Hi Sascha - Any time that access to your subobjects is not done through a common getattr(), you need to take an extra step to keep the acquisition chain intact. The Acquisition base classes (such as Acquisition.Implicit) override getattr() handling and automatically handle the wrapping of the resulting object when an attribute is accessed via getattr(). In your case, the resulting object is not being "wrapped" correctly since you are not accessing the object via getattr(). What you need to do is: a. make sure the objects in your _v_ dict are acquirers (i.e. that they have Acquisition.Implicit or another acquisition class as a base class) b. instead of returning the actual object that you get from your _v_ dict, you need to call the __of__ method of that object, passing 'self' as the acquisition 'container'. That will return the object "wrapped" in the context of "self" (the "container"). You will then return the _wrapped_ object from your __getitem__ method. A quick example: class Item(Acquisition.Implicit): # Just a simple item class def __init__(self, id): self.id=id class Container(Acquisition.Implicit): # A simple container. We have an _v_dict dictionary # where we keep some instances of Item. def __init__(self): # heres a dummy attribute that we'll define to # test to make sure our items acquire it... self.foobar='foobar' # create some Item instances in our _v_dict... self._v_dict={} self._v_dict['one']=Item('one') self._v_dict['two']=Item('two') def __getitem__(self, key): # implement getitem, and make sure that the resulting # objects are correctly wrapped by 'self' for acquisition. result=self._v_dict[key] result=result.__of__(self) return result Hope this helps! Brian Lloyd brian@digicool.com Software Engineer 540.371.6909 Digital Creations http://www.digicool.com
Brian Lloyd wrote:
(snip)
The Acquisition base classes (such as Acquisition.Implicit) override getattr() handling and automatically handle the wrapping of the resulting object when an attribute is accessed via getattr().
<note> This isn't quite right. ExtensionClass checks for an '__of__' method on a sub-object retrieved through an attribute access. The '__of__' method, is defined for the sub-object, is called to obtain the sub-object in the context of the container. Acquisition.Implicit defined '__of__', not '__getattr__'. The '__of__' protocol is *also* used for things like user-defined method types and computed attributes. </note> Jim -- Jim Fulton mailto:jim@digicool.com Python Powered! Technical Director (888) 344-4332 http://www.python.org Digital Creations http://www.digicool.com http://www.zope.org Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email address may not be added to any commercial mail list with out my permission. Violation of my privacy with advertising or SPAM will result in a suit for a MINIMUM of $500 damages/incident, $1500 for repeats.
participants (2)
-
Brian Lloyd -
Jim Fulton