[Zope3-dev] LocationProxy+None

Jeff Shell eucci.group at gmail.com
Tue Aug 14 10:15:48 EDT 2007

On 8/14/07, Adam Groszer <agroszer at gmail.com> wrote:
> A strange thing happened today:
> >>> x = LocationProxy(None, "foo", "bar")
> >>> x is None
> False
> >>> x == None
> True
> Is this OK or I'm just missing something?

You have to understand how Python's ``is`` differs from ``==``. An
`is` check is an identity check, not an equality check. It's generally
faster than comparing equality. It's basically like this::

   a is b if id(a) == id(b)

The Python `id` function is basically a memory address of that
specific instance. None, True, and False are all basically constants
that have the same identity unless that name is overridden in a local

When proxies enter the picture, you're wrapping the object with a
proxy, so it's no longer the same by identity.

    >>> from zope.location import LocationProxy
    >>> located_none = LocationProxy(None, "foo", "bar")
    >>> id(None)
    >>> id(located_none)
    >>> id(None) == id(located_none)
    >>> located_none is None

Since the proxy object is masquerading as its wrapped object, equality
checks works. But identity doesn't. The `zope.proxy` package includes
some functions that you can use for help.

    >>> from zope.proxy import removeAllProxies, sameProxiedObjects, isProxy
    >>> isProxy(None)
    >>> isProxy(located_none)

You have the option of removing all proxies, which would return the
object wrapped up by the proxy. Since we wrapped up None, it would
return the actual None object and identity checks would pass:

    >>> removeAllProxies(located_none) is None

Or you can use `sameProxiedObjects`, which does the identity check on
the internal objects of one or two proxies:

    >>> sameProxiedObjects(located_none, None)

You can see this when wrapping another instance of None with a location proxy.

    >>> singing_nun = LocationProxy(None, 'austria', 'jane')
    >>> id(singing_nun)
    >>> singing_nun is located_none
    >>> sameProxiedObjects(singing_nun, located_none)

Jeff Shell

More information about the Zope3-dev mailing list