[Zope] if object does not exist in python

Jonathan dev101 at magma.ca
Wed Dec 13 09:54:00 EST 2006


----- Original Message ----- 
From: "Andrew Milton" <akm at theinternet.com.au>
To: "Garito" <garito at sistes.net>
Cc: "zope" <Zope at zope.org>
Sent: Wednesday, December 13, 2006 9:40 AM
Subject: Re: [Zope] if object does not exist in python


> +-------[ Garito ]----------------------
> | Andrew Milton escribi?:
> | >+-------[ David Bear ]----------------------
> | >| Can anyone show me code examples of a script that checks for the
> | >| existences of a named object?
> | >
> | >if namedObject in container.objectIds():
> | >  # object exists
> | >else:
> | >  # doesn't
> | >
> | >
> | >Checks for named object inside the container that the script is in.
> | >
> | >  
> | hasattr(container, 'id')
> 
> That will acquire...

And the ever popular:

obj = context.restrictedTraverse('/path/to/object/objId', None)
if obj is None:
    # object not found
else:
    # object exists



Jonathan



More information about the Zope mailing list