[Zope3-Users] "boolean" value of adapted objects

Marius Gedminas mgedmin at b4net.lt
Tue Apr 3 17:29:48 EDT 2007


On Tue, Apr 03, 2007 at 12:46:25PM +0200, Thierry Florac wrote:
> I have a little question about "boolean" values of adapted objects.
> For example, I had the following code :
> 
> 	info = ISharedDataInfo(context, None) or \
> 	       IPrivateDataInfo(context, None)
> 	if info is not None:
> 	    return context
> 	return None
> 
> Even if "context" is correctly adapted to the first interface, the
> boolean value matching the adapted object is not always True ; in these
> cases, the second test is done, and the global result is set to "None"
> instead of "context".
> 
> Of course, it's easy to modify this code with two tests instead of one,
> but I'm sure there's an easy way to handle such things, so any
> information would be really great !!

There isn't.  Write the longer, more explicit code:

        info = ISharedDataInfo(context, None)
        if info is None:
            info = IPrivateDataInfo(context, None)

In your particular example, since you do not use info anywhere, you can
write

        if (ISharedDataInfo(context, None) is not None or
            IPrivateDataInfo(context, None) is not None):
            return context
        return None

or

        if (ISharedDataInfo(context, None) is None or
            IPrivateDataInfo(context, None) is None):
            return None
        return context

whichever seems clearer.

Marius Gedminas
-- 
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.zope.org/pipermail/zope3-users/attachments/20070404/85257907/attachment.bin


More information about the Zope3-users mailing list