pythonpython-2.7object-identityobject-equality

In Python (2.7), why is os.remove not identical to os.unlink?


>>> import sys
>>> sys.version
'2.7.3 (default, Mar 13 2014, 11:03:55) \n[GCC 4.7.2]'
>>> import os
>>> os.remove is os.unlink
False
>>> os.remove == os.unlink
True

Why is that? Isn't os.unlink supposed to be an alias of os.remove?


Solution

  • To answer this question we have to dive a bit into the details of how the python interpreter works. It might be different in other python implementations.

    First let's start where the os.remove and os.unlink functions are defined. In Modules/posixmodule.c they are registered as:

    {"unlink",          posix_unlink, METH_VARARGS, posix_unlink__doc__},
    {"remove",          posix_unlink, METH_VARARGS, posix_remove__doc__},
    

    Note that the function pointers both point to posix_unlink in their ml_meth member.

    For method objects, the == equality operator is implemented by meth_richcompare(...) in Objects/methodobject.c.

    It contains this logic, which explains why the == operator returns True.

    a = (PyCFunctionObject *)self;
    b = (PyCFunctionObject *)other;
    eq = a->m_self == b->m_self;
    if (eq)
        eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
    

    For built-in functions m_self is NULL so eq starts out true. We then compare the function pointers in ml_meth (the same posix_unlink referenced from the struct above) and since they match eq remains true. The end result is that python returns True.

    The is operator is simpler and stricter. The is operator only compares the PyCFunctionObj* pointers. They will be different -- they came from different structs and are distinct objects, so the is operator will return False.

    The rationale is likely that they are separate functions objects (recall their docstrings are different) but they point to the same implementation, so the difference in behavior between is and == is justifiable.

    is brings a stronger guarantee, and is meant to be fast and cheap (a pointer comparison, essentially). The == operator inspects the object and returns True when its content matches. In this context, the function pointer is the content.