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 @@ -87,6 +87,13 @@ truth tests, identity tests, and boolean operations:
.. versionadded:: 3.14


.. function:: is_not_none(a)

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

.. versionadded:: 3.14


The mathematical and bitwise operations are the most numerous:


Expand Down Expand Up @@ -414,6 +421,8 @@ Python syntax and the functions in the :mod:`operator` module.
+-----------------------+-------------------------+---------------------------------------+
| Identity | ``a is None`` | ``is_none(a)`` |
+-----------------------+-------------------------+---------------------------------------+
| Identity | ``a is not None`` | ``is_not_none(a)`` |
+-----------------------+-------------------------+---------------------------------------+
| Indexed Assignment | ``obj[k] = v`` | ``setitem(obj, k, v)`` |
+-----------------------+-------------------------+---------------------------------------+
| Indexed Deletion | ``del obj[k]`` | ``delitem(obj, k)`` |
Expand Down
6 changes: 4 additions & 2 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ of the error.
operator
--------

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

os
Expand Down
8 changes: 6 additions & 2 deletions Lib/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
'delitem', 'eq', 'floordiv', 'ge', 'getitem', 'gt', 'iadd', 'iand',
'iconcat', 'ifloordiv', 'ilshift', 'imatmul', 'imod', 'imul',
'index', 'indexOf', 'inv', 'invert', 'ior', 'ipow', 'irshift',
'is_', 'is_none', 'is_not', 'isub', 'itemgetter', 'itruediv', 'ixor', 'le',
'length_hint', 'lshift', 'lt', 'matmul', 'methodcaller', 'mod',
'is_', 'is_none', 'is_not', 'is_not_none', '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 @@ -70,6 +70,10 @@ def is_none(a):
"Same as a is None."
return a is None

def is_not_none(a):
"Same as a is not None."
return a is not 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 @@ -357,6 +357,16 @@ def test_is_none(self):
self.assertFalse(operator.is_none(b))
self.assertTrue(operator.is_none(c))

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

def test_attrgetter(self):
operator = self.module
class A:
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Add :func:`operator.is_none` function.
Add :func:`operator.is_none` and :func:`operator.is_not_none` functions.
15 changes: 15 additions & 0 deletions Modules/_operator.c
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,20 @@ _operator_is_none(PyObject *module, PyObject *a)
return Py_NewRef(result);
Comment thread
Fidget-Spinner marked this conversation as resolved.
Resolved
}

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

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

static PyObject *
_operator_is_not_none(PyObject *module, PyObject *a)
/*[clinic end generated code: output=b0168a51451d9140 input=7587f38ebac51688]*/
{
PyObject *result = Py_IsNotNone(a) ? Py_True : Py_False;
Comment thread
Eclips4 marked this conversation as resolved.
Resolved
return Py_NewRef(result);
}

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

/*
Expand Down Expand Up @@ -931,6 +945,7 @@ static struct PyMethodDef operator_methods[] = {
_OPERATOR_IS__METHODDEF
_OPERATOR_IS_NOT_METHODDEF
_OPERATOR_IS_NONE_METHODDEF
_OPERATOR_IS_NOT_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.