I have a zope containment hierarchy base/somefolder/myobj base/other base, somefolder, myobj, and other are all instances of a MyFolder class which is derived from Folder. MyFolder is a simple extension of Folder that limits the allowed meta classes. I would like to acquire the 'other' folder from a myobj instance using the python API. Ie, I have a class method in which I need to be able to do def somemethod(self, someinstance): otherFolder = acquisition_magic(someinstance, 'other') where I know someinstance is a MyFolder instance which is either base itself, is contained in base (possibly deep in the tree). Ie, I would like this method to work if someinstance is one of base, somefolder, or myobj. Since all of these instances derive from Acquisition.Implicit via ObjectManager, I think I should be able to do this. I thought from Acquisition import aq_get, aq_acquire, aq_parent def somemethod(self, someinstance): folder = aq_acquire(someinstance, 'other') would do it for me, but this raises an AttributeError when passed a myobj instance. Thanks! JDH zope 2.7
On Friday 18 June 2004 09:27 am, John Hunter wrote:
I have a zope containment hierarchy base/somefolder/myobj base/other
I would like to acquire the 'other' folder from a myobj instance using the python API. Ie, I have a class method in which I need to be able to do
def somemethod(self, someinstance): otherFolder = acquisition_magic(someinstance, 'other')
So "somemethod" is a class-method of "myobj"? Then "self" is the myobj instance. It will acquire the attributes of its container if it inherits from Acquisition.Implicit, which if it subclasses Folder, it does. That means you can refer to the "other" instance as: self.other or getattr(self, 'other') You don't need to use the explicit acquisition methods unless you want to do something fancier like unwrap/re-wrap the instance with a new acquisition wrapper or something (and it seems unlikely that you need to). (This is in product code -- i.e. you can use this within a method of your MyFolder class). I'm a little unclear on why you need to pass another instance to your method (especially if it is the same instance so that self is someinstance is true). Anyway, I *think* this answers your question, unless there's a subtlety here that I'm missing. Cheers, Terry -- Terry Hancock ( hancock at anansispaceworks.com ) Anansi Spaceworks http://www.anansispaceworks.com
"Terry" == Terry Hancock <hancock@anansispaceworks.com> writes:
Terry> So "somemethod" is a class-method of "myobj"? Then "self" Terry> is the myobj instance. It will acquire the attributes of Terry> its container if it inherits from Acquisition.Implicit, Terry> which if it subclasses Folder, it does. I finally figured out the cause of my problem. self in the __init__ function of a class is not the same as self in a class method, like index_html. The latter is wrapped in an Acquisition wrapper. I was trying to acquire from the class __init__ function, which was not wrapped, and hence my troubles. All is well (for now...) Thanks, JDH
participants (2)
-
John Hunter -
Terry Hancock