[Zope3-dev] LocationProxy+None
Adam Groszer
agroszer at gmail.com
Tue Aug 14 13:11:49 EDT 2007
Thank you Jeff for the clarification.
Thought so that could be something like this, but was not so clear on
spot.
Tuesday, August 14, 2007, 4:15:48 PM, you wrote:
JS> 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?
JS> You have to understand how Python's ``is`` differs from ``==``. An
JS> `is` check is an identity check, not an equality check. It's generally
JS> faster than comparing equality. It's basically like this::
JS> a is b if id(a) == id(b)
JS> The Python `id` function is basically a memory address of that
JS> specific instance. None, True, and False are all basically constants
JS> that have the same identity unless that name is overridden in a local
JS> namespace.
JS> When proxies enter the picture, you're wrapping the object with a
JS> proxy, so it's no longer the same by identity.
JS> >>> from zope.location import LocationProxy
JS> >>> located_none = LocationProxy(None, "foo", "bar")
JS> >>> id(None)
JS> 3120456
JS> >>> id(located_none)
JS> 404616
JS> >>> id(None) == id(located_none)
JS> False
JS> >>> located_none is None
JS> False
JS> Since the proxy object is masquerading as its wrapped object, equality
JS> checks works. But identity doesn't. The `zope.proxy` package includes
JS> some functions that you can use for help.
JS> >>> from zope.proxy import removeAllProxies, sameProxiedObjects, isProxy
JS> >>> isProxy(None)
JS> False
JS> >>> isProxy(located_none)
JS> True
JS> You have the option of removing all proxies, which would return the
JS> object wrapped up by the proxy. Since we wrapped up None, it would
JS> return the actual None object and identity checks would pass:
JS> >>> removeAllProxies(located_none) is None
JS> True
JS> Or you can use `sameProxiedObjects`, which does the identity check on
JS> the internal objects of one or two proxies:
JS> >>> sameProxiedObjects(located_none, None)
JS> True
JS> You can see this when wrapping another instance of None with a location proxy.
JS> >>> singing_nun = LocationProxy(None, 'austria', 'jane')
JS> >>> id(singing_nun)
JS> 7378424
JS> >>> singing_nun is located_none
JS> False
JS> >>> sameProxiedObjects(singing_nun, located_none)
JS> True
--
Best regards,
Groszer Adam
--
Quote of the day:
Doubters invert the metaphor and insist that they need faith as big as
a mountain in order to move a mustard seed.
- Anonymous
More information about the Zope3-dev
mailing list