>>> 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?
unlink() method in Python is used to remove or delete a file path. This method is semantically identical to os. remove() method.
remove() method. To avoid getting an error while deleting a file, use the os. path. exists() before executing the os.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With