Effects of caching frequently used objects, was Re: Explaining names vs variables in Python

Chris Angelico rosuav at gmail.com
Fri Mar 25 09:22:33 EDT 2016


On Sat, Mar 26, 2016 at 12:03 AM, Albert-Jan Roskam
<sjeik_appie at hotmail.com> wrote:
>> You should not bother with object identity for objects other than None.
>
>
> A little late to the party, but: how about Ellipsis? Shouldn't "is" also be used for that one? (It's rare, I know :))

Yes, and also True and False, if you're checking for the specific
values. (Though it's more common in Python to merely care about
truthiness/falsiness.) Other common identity checks include:

if str is bytes:
    # Python 2 handling, where "native strings" are byte strings
else:
    # Python 3 handling, where "native strings" are text strings

if iter(x) is x:
    # x is an iterator, not some other iterable

_SENTINEL = object()
def func(arg=_SENTINEL):
    if arg is _SENTINEL:
        # arg was not passed

Anyone who says that identity checks are only for None is
significantly over-simplifying things. This isn't necessarily a bad
thing, but it should never be taken to be the whole story.

ChrisA



More information about the Python-list mailing list