Return ``a is not b``. Tests object identity.
+.. function:: is_none(a)
+
+ Return ``a is None``. Tests object identity.
+
+ .. 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:
+-----------------------+-------------------------+---------------------------------------+
| Identity | ``a is not b`` | ``is_not(a, b)`` |
+-----------------------+-------------------------+---------------------------------------+
+| 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)`` |
of the error.
(Contributed by Serhiy Storchaka in :gh:`122163`.)
+operator
+--------
+
+* 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
--
'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',
- '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']
"Same as a is not b."
return a is not b
+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):
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_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:
--- /dev/null
+Add :func:`operator.is_none` and :func:`operator.is_not_none` functions.
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);
+}
+
+/*[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_IsNone(a) ? Py_False : Py_True;
+ return Py_NewRef(result);
+}
+
/* compare_digest **********************************************************/
/*
_OPERATOR_COUNTOF_METHODDEF
_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
return return_value;
}
+PyDoc_STRVAR(_operator_is_none__doc__,
+"is_none($module, a, /)\n"
+"--\n"
+"\n"
+"Same as a is None.");
+
+#define _OPERATOR_IS_NONE_METHODDEF \
+ {"is_none", (PyCFunction)_operator_is_none, METH_O, _operator_is_none__doc__},
+
+PyDoc_STRVAR(_operator_is_not_none__doc__,
+"is_not_none($module, a, /)\n"
+"--\n"
+"\n"
+"Same as a is not None.");
+
+#define _OPERATOR_IS_NOT_NONE_METHODDEF \
+ {"is_not_none", (PyCFunction)_operator_is_not_none, METH_O, _operator_is_not_none__doc__},
+
PyDoc_STRVAR(_operator_length_hint__doc__,
"length_hint($module, obj, default=0, /)\n"
"--\n"
exit:
return return_value;
}
-/*[clinic end generated code: output=ddbba2cd943571eb input=a9049054013a1b77]*/
+/*[clinic end generated code: output=972e2543c4fcf1ba input=a9049054013a1b77]*/