Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 299
Kal Avatar asked Dec 02 '14 02:12

Kal


People also ask

What is the difference between OS remove and OS unlink?

unlink() method in Python is used to remove or delete a file path. This method is semantically identical to os. remove() method.

How will you handle the error that you encounter while deleting the file in Python?

remove() method. To avoid getting an error while deleting a file, use the os. path. exists() before executing the os.


1 Answers

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.

like image 87
davvid Avatar answered Sep 24 '22 15:09

davvid