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

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


        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?

Attachment: signature.asc
Description: Digital signature

Zope3-users mailing list

Reply via email to