4
This module exports a set of functions corresponding to the intrinsic5
operators of Python. For example, operator.add(x, y) is equivalent6
to the expression x+y. The function names are those used for special7
methods; variants without leading and trailing '__' are also provided10
This is the pure Python implementation of the module.13
__all__ = ['abs', 'add', 'and_', 'attrgetter', 'call', 'concat', 'contains', 'countOf',14
'delitem', 'eq', 'floordiv', 'ge', 'getitem', 'gt', 'iadd', 'iand',15
'iconcat', 'ifloordiv', 'ilshift', 'imatmul', 'imod', 'imul',16
'index', 'indexOf', 'inv', 'invert', 'ior', 'ipow', 'irshift',17
'is_', 'is_none', 'is_not', 'is_not_none', 'isub', 'itemgetter', 'itruediv',18
'ixor', 'le', 'length_hint', 'lshift', 'lt', 'matmul', 'methodcaller', 'mod',19
'mul', 'ne', 'neg', 'not_', 'or_', 'pos', 'pow', 'rshift',20
'setitem', 'sub', 'truediv', 'truth', 'xor']22
from builtins import abs as _abs25
# Comparison Operations *******************************************************#51
# Logical Operations **********************************************************#58
"Return True if a is true, False otherwise."59
return True if a else False74
"Same as a is not None."77
# Mathematical/Bitwise Operations *********************************************#96
"Same as a.__index__()."152
# Sequence Operations *********************************************************#155
"Same as a + b, for a and b sequences."156
if not hasattr(a, '__getitem__'):157
msg = "'%s' object can't be concatenated" % type(a).__name__162
"Same as b in a (note reversed operands)."166
"Return the number of items in a which are, or which equal, b."182
"Return the first index of b in a."183
for i, j in enumerate(a):187
raise ValueError('sequence.index(x): x not in sequence')193
def length_hint(obj, default=0):195
Return an estimate of the number of items in obj.196
This is useful for presizing containers when building from an iterable.198
If the object supports len(), the result will be exact. Otherwise, it may199
over- or under-estimate by an arbitrary amount. The result will be an202
if not isinstance(default, int):203
msg = ("'%s' object cannot be interpreted as an integer" %204
type(default).__name__)213
hint = type(obj).__length_hint__214
except AttributeError:221
if val is NotImplemented:223
if not isinstance(val, int):224
msg = ('__length_hint__ must be integer, not %s' %228
msg = '__length_hint__() should return >= 0'229
raise ValueError(msg)232
# Other Operations ************************************************************#234
def call(obj, /, *args, **kwargs):235
"""Same as obj(*args, **kwargs)."""236
return obj(*args, **kwargs)238
# Generalized Lookup Objects **************************************************#242
Return a callable object that fetches the given attribute(s) from its operand.243
After f = attrgetter('name'), the call f(r) returns r.name.244
After g = attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).245
After h = attrgetter('name.first', 'name.last'), the call h(r) returns246
(r.name.first, r.name.last).248
__slots__ = ('_attrs', '_call')250
def __init__(self, attr, /, *attrs):252
if not isinstance(attr, str):253
raise TypeError('attribute name must be a string')254
self._attrs = (attr,)255
names = attr.split('.')258
obj = getattr(obj, name)262
self._attrs = (attr,) + attrs263
getters = tuple(map(attrgetter, self._attrs))265
return tuple(getter(obj) for getter in getters)268
def __call__(self, obj, /):269
return self._call(obj)272
return '%s.%s(%s)' % (self.__class__.__module__,273
self.__class__.__qualname__,274
', '.join(map(repr, self._attrs)))276
def __reduce__(self):277
return self.__class__, self._attrs281
Return a callable object that fetches the given item(s) from its operand.282
After f = itemgetter(2), the call f(r) returns r[2].283
After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])285
__slots__ = ('_items', '_call')287
def __init__(self, item, /, *items):289
self._items = (item,)294
self._items = items = (item,) + items296
return tuple(obj[i] for i in items)299
def __call__(self, obj, /):300
return self._call(obj)303
return '%s.%s(%s)' % (self.__class__.__module__,304
self.__class__.__name__,305
', '.join(map(repr, self._items)))307
def __reduce__(self):308
return self.__class__, self._items312
Return a callable object that calls the given method on its operand.313
After f = methodcaller('name'), the call f(r) returns r.name().314
After g = methodcaller('name', 'date', foo=1), the call g(r) returns315
r.name('date', foo=1).317
__slots__ = ('_name', '_args', '_kwargs')319
def __init__(self, name, /, *args, **kwargs):321
if not isinstance(self._name, str):322
raise TypeError('method name must be a string')324
self._kwargs = kwargs326
def __call__(self, obj, /):327
return getattr(obj, self._name)(*self._args, **self._kwargs)330
args = [repr(self._name)]331
args.extend(map(repr, self._args))332
args.extend('%s=%r' % (k, v) for k, v in self._kwargs.items())333
return '%s.%s(%s)' % (self.__class__.__module__,334
self.__class__.__name__,337
def __reduce__(self):339
return self.__class__, (self._name,) + self._args341
from functools import partial342
return partial(self.__class__, self._name, **self._kwargs), self._args345
# In-place Operations *********************************************************#358
"Same as a += b, for a and b sequences."359
if not hasattr(a, '__getitem__'):360
msg = "'%s' object can't be concatenated" % type(a).__name__422
from _operator import *426
from _operator import __doc__ # noqa: F401428
# All of these "__func__ = func" assignments have to happen after importing429
# from _operator to make sure they're set to the right function441
# __floordiv__ = floordiv455
# __truediv__ = truediv458
# __contains__ = contains459
# __delitem__ = delitem460
# __getitem__ = getitem461
# __setitem__ = setitem464
# __iconcat__ = iconcat465
# __ifloordiv__ = ifloordiv466
# __ilshift__ = ilshift469
# __imatmul__ = imatmul472
# __irshift__ = irshift474
# __itruediv__ = itruediv