[Zope] How to test if an object exist

Dieter Maurer dieter@handshake.de
Thu, 24 Aug 2000 00:42:39 +0200 (CEST)


Francois-Regis CHALAOUX writes:
 > Errot Type: NameError
 > Error Value: folder1
 > 
 > How to manage this problem ?
 > 
 > FR.
 > 
 > =====
 > CODE
 > =====
 > def testFolder(self):
 >     self=self.this()
 >     sub1=self.folder1
 >     exec "object2create = 'folder11'"
 >     try:
 >         if sub1.object2create:
 >             return 'Object exists'
 >     except AttributeError:
 >         sub1.manage_addFolder(object2create)
 >         return 'Object created'
Are you sure, you get the "NameError" inside your external method?

I hope, you are not, because the method cannot raise a (NameError,folder1).

Anyway, if your problem is in Python code, the traceback contains
precise line number information for the problem.


In order to test, whether an object O can access another object o,
you can use: "hasattr(O,'o')".

In your example:

	if hasattr(self,'folder1'): return 'Object exists'

This will not test, that 'folder1' is contained in "self"
but that a 'folder1' is accessible (acquired) in "self".

In order to test for direct containment, you would use
"self.aq_base" instead of "self".



Dieter