[Zope] Acquisition on dynamically created objects
Brian Lloyd
Brian@digicool.com
Sun, 16 Jan 2000 11:59:38 -0500
> 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