[Zope3-Users] Using 'if object' evaluates false even if object exists

Giovannetti, Mark giovanne at nrcan.gc.ca
Thu Apr 12 15:30:27 EDT 2007


Hi list,

Hope everyone's having a splendid day/evening/whatever.

I've encountered a strange error.  I have two chunks
of nearly identical code below.  The only difference
is the first "if" statement.  One has:

    if context:

the other has:

    if context is not None

The first fails to be true even if context is not None.
This can be seen by the output from the logs showing the
context variable as:

7777777777777777777 Context
<books.finance.fiscalyear.FiscalYearSQLContainer object at 0xad90aac>
TitledAbsoluteURL.breadcrumbs, calling getBreadCrumbTitle
getBreadCrumbTitle, no context.
 
When I changed it to the second form (i.e. not None) the
context is OK and the code works.  Why does the object
'context' not evaluate to true?  This is a nasty gotcha that
I'd like to understand.

I thought that any object that exists (and doesn't equal
zero, '' or False) resolves to true.  Can anyone help?

I'm still pretty new to python and zope so be gentle.

I've made sure that the proper parameters are being passed
to the function and so on.

Thanks in advance!

Mark

P.S. I hope there wasn't any horrible wrapping...

================ didn't work =============
============= source ==============
def getBreadCrumbTitle(context=None, default=None):
    """
    Helper function to get a title for an URL breadcrumb list.
    """
    title = None
    if context:
        print "55555555555555555555", removeSecurityProxy(context)
        title = getattr(context, 'title', None)

        if title is None:
            print "Title not an attribute of context, setting to
default."
            title = default
            try:
                annotations = IZopeDublinCore(context)
                if annotations:
                    print "Annotations", annotations
                    try:
                        title = annotations.title
                        print "Annotation's Title", annotations.title
                    except KeyError:
                        print "Annotation's Title KeyError"
                        pass
                else:
                    print "Annotations adapted but empty."

            except TypeError:
                print "Annotations not available-No Adaption to Dublin
Core"
                pass
        else:
            print "getBreadCrumbTitle, Title not None-gotten from
getattr"

    else:
        print "getBreadCrumbTitle, no context."

    if title is None:
        raise TypeError(_insufficientTitleContext)
    else:
        print "getBreadCrumbTitle, Title is not None", title

    return title

============== log ===================
9999999999999999 {'__name__': u'FY', '_class': <class
'books.finance.fiscalyear.FiscalYear'>, '_className':
u'books.finance.fiscalyear.FiscalYear', '__parent__':
<zope.app.folder.folder.Folder object at 0xad747ac>}
TitledSiteAbsoluteURL.breadcrumbs, calling getBreadCrumbTitle
55555555555555555555 <zope.app.folder.folder.Folder object at 0xad747ac>
Title not an attribute of context, setting to default.
Annotations <zope.dublincore.annotatableadapter.ZDCAnnotatableAdapter
object at 0xadd8cac>
Annotation's Title huh
getBreadCrumbTitle, Title is not None huh
7777777777777777777 Context
<books.finance.fiscalyear.FiscalYearSQLContainer object at 0xad90aac>
TitledAbsoluteURL.breadcrumbs, calling getBreadCrumbTitle
getBreadCrumbTitle, no context.

==========================================


================ worked =============
================ source =============
def getBreadCrumbTitle(context=None, default=None):
    """
    Helper function to get a title for an URL breadcrumb list.
    """
    title = None
    if context is not None:
        print "55555555555555555555", removeSecurityProxy(context)
        title = getattr(context, 'title', None)

        if title is None:
            print "Title not an attribute of context, setting to
default."
            title = default
            try:
                annotations = IZopeDublinCore(context)
                if annotations:
                    print "Annotations", annotations
                    try:
                        title = annotations.title
                        print "Annotation's Title", annotations.title
                    except KeyError:
                        print "Annotation's Title KeyError"
                        pass
                else:
                    print "Annotations adapted but empty."

            except TypeError:
                print "Annotations not available-No Adaption to Dublin
Core"
                pass
        else:
            print "getBreadCrumbTitle, Title not None-gotten from
getattr"

    else:
        print "444444444444444444444", removeSecurityProxy(context)
        print "getBreadCrumbTitle, no context."

    if title is None:
        raise TypeError(_insufficientTitleContext)
    else:
        print "getBreadCrumbTitle, Title is not None", title

    return title

================== log ================
9999999999999999 {'__name__': u'FY', '_class': <class
'books.finance.fiscalyear.FiscalYear'>, '_className':
u'books.finance.fiscalyear.FiscalYear', '__parent__':
<zope.app.folder.folder.Folder object at 0x93fddec>}
TitledSiteAbsoluteURL.breadcrumbs, calling getBreadCrumbTitle
55555555555555555555 <zope.app.folder.folder.Folder object at 0x93fddec>
Title not an attribute of context, setting to default.
Annotations <zope.dublincore.annotatableadapter.ZDCAnnotatableAdapter
object at 0xad1078c>
Annotation's Title huh
getBreadCrumbTitle, Title is not None huh
7777777777777777777 Context
<books.finance.fiscalyear.FiscalYearSQLContainer object at 0xacb292c>
TitledAbsoluteURL.breadcrumbs, calling getBreadCrumbTitle
55555555555555555555 <books.finance.fiscalyear.FiscalYearSQLContainer
object at 0xacb292c>
Title not an attribute of context, setting to default.
Annotations not available-No Adaption to Dublin Core
getBreadCrumbTitle, Title is not None FY
======================================


More information about the Zope3-users mailing list