Skip to content
Merged
9 changes: 9 additions & 0 deletions Doc/library/operator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ truth tests, identity tests, and boolean operations:
Return ``a is not b``. Tests object identity.


.. function:: is_none(a)

Return ``a is None``. Tests object identity.

.. versionadded:: 3.13
Comment thread
ThexXTURBOXx marked this conversation as resolved.
Resolved


The mathematical and bitwise operations are the most numerous:


Expand Down Expand Up @@ -405,6 +412,8 @@ Python syntax and the functions in the :mod:`operator` module.
+-----------------------+-------------------------+---------------------------------------+
| Identity | ``a is not b`` | ``is_not(a, b)`` |
+-----------------------+-------------------------+---------------------------------------+
| Identity | ``a is None`` | ``is_none(a)`` |
+-----------------------+-------------------------+---------------------------------------+
| Indexed Assignment | ``obj[k] = v`` | ``setitem(obj, k, v)`` |
+-----------------------+-------------------------+---------------------------------------+
| Indexed Deletion | ``del obj[k]`` | ``delitem(obj, k)`` |
Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,13 @@ opcode
documented or exposed through ``dis``, and were not intended to be
used externally.

operator
--------

* A new function ``operator.is_none`` has been added, such that
``operator.is_none(obj)`` is equivalent to ``obj is None``.
(Contributed by Raymond Hettinger and Nico Mexis in :gh:`115808`.)

Comment thread
ThexXTURBOXx marked this conversation as resolved.
Resolved
os
--

Expand Down
6 changes: 5 additions & 1 deletion Lib/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
'delitem', 'eq', 'floordiv', 'ge', 'getitem', 'gt', 'iadd', 'iand',
'iconcat', 'ifloordiv', 'ilshift', 'imatmul', 'imod', 'imul',
'index', 'indexOf', 'inv', 'invert', 'ior', 'ipow', 'irshift',
'is_', 'is_not', 'isub', 'itemgetter', 'itruediv', 'ixor', 'le',
'is_', 'is_none', 'is_not', 'isub', 'itemgetter', 'itruediv', 'ixor', 'le',
'length_hint', 'lshift', 'lt', 'matmul', 'methodcaller', 'mod',
'mul', 'ne', 'neg', 'not_', 'or_', 'pos', 'pow', 'rshift',
'setitem', 'sub', 'truediv', 'truth', 'xor']
Expand Down Expand Up @@ -66,6 +66,10 @@ def is_not(a, b):
"Same as a is not b."
return a is not b

def is_none(a):
"Same as a is None."
return a is None

# Mathematical/Bitwise Operations *********************************************#

def abs(a):
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,16 @@ def test_is_not(self):
self.assertFalse(operator.is_not(a, b))
self.assertTrue(operator.is_not(a,c))

def test_is_none(self):
operator = self.module
a = 'xyzpdq'
b = ''
c = None
self.assertRaises(TypeError, operator.is_none)
self.assertFalse(operator.is_none(a))
self.assertFalse(operator.is_none(b))
self.assertTrue(operator.is_none(c))

def test_attrgetter(self):
operator = self.module
class A:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add :func:`operator.is_none` function.
15 changes: 15 additions & 0 deletions Modules/_operator.c
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,20 @@ _operator_is_not_impl(PyObject *module, PyObject *a, PyObject *b)
return Py_NewRef(result);
}

/*[clinic input]
_operator.is_none = _operator.neg

Same as a is None.
[clinic start generated code]*/

static PyObject *
_operator_is_none(PyObject *module, PyObject *a)
/*[clinic end generated code: output=07159cc102261dec input=0448b38af7b8533d]*/
{
PyObject *result = Py_IsNone(a) ? Py_True : Py_False;
return Py_NewRef(result);
Comment thread
Fidget-Spinner marked this conversation as resolved.
Resolved
}

/* compare_digest **********************************************************/

/*
Expand Down Expand Up @@ -916,6 +930,7 @@ static struct PyMethodDef operator_methods[] = {
_OPERATOR_COUNTOF_METHODDEF
_OPERATOR_IS__METHODDEF
_OPERATOR_IS_NOT_METHODDEF
_OPERATOR_IS_NONE_METHODDEF
_OPERATOR_INDEX_METHODDEF
_OPERATOR_ADD_METHODDEF
_OPERATOR_SUB_METHODDEF
Expand Down
11 changes: 10 additions & 1 deletion Modules/clinic/_operator.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.