[Zope] How to see if two objects are the same (minor fix)

Dieter Maurer dieter@handshake.de
Thu, 12 Oct 2000 09:59:43 +0200 (CEST)


Ron Bickers writes:
 > I want to see if an object is the one aqcuired from the root, but I'm
 > hitting walls.
 > 
 >   <dtml-var "PARENTS[-1].myObject">
 >   <dtml-var "myObject">
 >   <dtml-var "PARENTS[-1].myObject is myObject">
 > 
 > This returns the following when in the root folder context:
 > 
 >   <TinyTable instance at 8684ad8>
 >   <TinyTable instance at 8684ad8>
 >   0
What you see here, is Zope's (more precisely ExtensionClass's)
acquisition magic:

	when you access "object.attribute", you (usually)
	will not get the object corresponding to
	"attribute" but a new object:

		"attribute.__of__(object)"

	i.e. the attribute object in the context of
	"object".

	When you look up an attribute of this object,
	it will first in the object itself and
	when it does not success there, it will look
	in the context.
	That is what you like with acquisition.

	The downside:

	  When you access the "same" object via different
	  contexts, you will in fact get different objects
	  (as the contexts are different).
	  "is" will return false, as it should be.

	But, even, if you use "==", you will get "false".
	There have been some discussion in the list, whether
	this is the best behaviour.

What can you do?

	You can use the "aq_base" attribute to strip
	away any context and get to the base object.

	At least in Zope 2.2.1, "aq_base" was not exposed
	to DTML. Thus, you would need an external method,
	say "getBaseObject", to get at the base:

		def getBaseObject(object):
			return getattr(object,'aq_base',object)

        With this function, you can compare the base objects:

	  <dtml-var "getBaseObject(myvar) is getBaseObject(PARENT[-1].myvar)">

       Be careful, however:
         Even, if such a test returns true for two objects,
	 the objects may nevertheless have
	 different behaviour (as the contexts are different).


Dieter