[Zope] Zope context

Chris Withers chrisw@nipltd.com
Mon, 05 Aug 2002 16:58:02 +0100


DA Loeffler wrote:
> I got round this one by actually passing the context to the class
> constructor as an argument, and storing it as self.context with the
> class's __init__ method. You can then freely call things on it, in my
> case

Ouch! Bad! Pain! Don't!

> for r in self.context.someSQLMethod():
> 
> or whatever. Since python functions are call-by-reference, you can
> even do self.context.manage_addFolder() or whatever, and it will affect
> the context originially passed to it.

This is distinctly dodgy and probably work more by luck than design :-S

>>Somebody know how to get a Zope Context within Zope Product or External 
>>Method? 
>>
>>For example, I have an object (e.g. DTML Method) somewhere in Zope, called as 
>>"MyObject". It's might be accessed: http://www.myhost.com/test/MyObject
>>
>>The problem is, that External Method like this works O.K.:
>>
>>[test.py (External Method)]
>>--------8<----------
>>def foo(self):
>>	return self.test.MyObject()
>>--------8<----------

Indeed...


>>...but *within a class* -- not:
>>[test.py (External Method)]
>>--------8<----------
>>class bar:
>>	def spam(self):
>>		return self.test.MyObject()
>>
>>def foo(self):
>>	b = bar()
>>	return b.spam()
>>--------8<----------

Well, creating class instances in external methods like this is distinctly dodgy.

Perhaps you could explain what you're trying to do and we can suggest the 
'right' way of going about it?

cheers,

Chris