Movatterモバイル変換


[0]ホーム

URL:


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:3.9 UserStringUp:3. Python Runtime ServicesNext:3.10.1 Mapping Operators to

3.10operator -- Standard operators as functions.

Theoperator module exports a set of functions implemented in Ccorresponding to the intrinsic operators of Python. For example,operator.add(x, y) is equivalent to the expressionx+y. Thefunction names are those used for special class methods; variants withoutleading and trailing "__" are also provided for convenience.

The functions fall into categories that perform object comparisons,logical operations, mathematical operations, sequence operations, andabstract type tests.

The object comparison functions are useful for all objects, and arenamed after the rich comparison operators they support:

lt(a, b)
le(a, b)
eq(a, b)
ne(a, b)
ge(a, b)
gt(a, b)
__lt__(a, b)
__le__(a, b)
__eq__(a, b)
__ne__(a, b)
__ge__(a, b)
__gt__(a, b)
Perform ``rich comparisons'' betweena andb. Specifically,lt(a,b) is equivalent toa <b,le(a,b) is equivalent toa <=b,eq(a,b) is equivalent toa ==b,ne(a,b) is equivalent toa !=b,gt(a,b) is equivalent toa >bandge(a,b) is equivalent toa >=b.Note that unlike the built-incmp(), these functions canreturn any value, which may or may not be interpretable as a Booleanvalue. See thePython Reference Manualfor more informations about rich comparisons.New in version 2.2.

The logical operations are also generally applicable to all objects,and support truth tests and Boolean operations:

not_(o)
__not__(o)
Return the outcome ofnoto. (Note that there is no__not__() method for object instances; only the interpretercore defines this operation. The result is affected by the__nonzero__() and__len__() methods.)

truth(o)
Return1 ifo is true, and 0 otherwise.

The mathematical and bitwise operations are the most numerous:

abs(o)
__abs__(o)
Return the absolute value ofo.

add(a, b)
__add__(a, b)
Returna+b, fora andb numbers.

and_(a, b)
__and__(a, b)
Return the bitwise and ofa andb.

div(a, b)
__div__(a, b)
Returna/b when__future__.division is notin effect. This is also known as ``classic'' division.

floordiv(a, b)
__floordiv__(a, b)
Returna//b.New in version 2.2.

inv(o)
invert(o)
__inv__(o)
__invert__(o)
Return the bitwise inverse of the numbero. This is equivalentto~o. The namesinvert() and__invert__() were added in Python 2.0.

lshift(a, b)
__lshift__(a, b)
Returna shifted left byb.

mod(a, b)
__mod__(a, b)
Returna%b.

mul(a, b)
__mul__(a, b)
Returna*b, fora andb numbers.

neg(o)
__neg__(o)
Returno negated.

or_(a, b)
__or__(a, b)
Return the bitwise or ofa andb.

pos(o)
__pos__(o)
Returno positive.

rshift(a, b)
__rshift__(a, b)
Returna shifted right byb.

sub(a, b)
__sub__(a, b)
Returna-b.

truediv(a, b)
__truediv__(a, b)
Returna/b when__future__.division is ineffect. This is also known as division.New in version 2.2.

xor(a, b)
__xor__(a, b)
Return the bitwise exclusive or ofa andb.

Operations which work with sequences include:

concat(a, b)
__concat__(a, b)
Returna+b fora andb sequences.

contains(a, b)
__contains__(a, b)
Return the outcome of the testbina.Note the reversed operands. The name__contains__() wasadded in Python 2.0.

countOf(a, b)
Return the number of occurrences ofb ina.

delitem(a, b)
__delitem__(a, b)
Remove the value ofa at indexb.

delslice(a, b, c)
__delslice__(a, b, c)
Delete the slice ofa from indexb to indexc-1.

getitem(a, b)
__getitem__(a, b)
Return the value ofa at indexb.

getslice(a, b, c)
__getslice__(a, b, c)
Return the slice ofa from indexb to indexc-1.

indexOf(a, b)
Return the index of the first of occurrence ofb ina.

repeat(a, b)
__repeat__(a, b)
Returna*b wherea is a sequence andb is an integer.

sequenceIncludes(...)
Deprecated since release 2.0.Usecontains() instead.

Alias forcontains().

setitem(a, b, c)
__setitem__(a, b, c)
Set the value ofa at indexb toc.

setslice(a, b, c, v)
__setslice__(a, b, c, v)
Set the slice ofa from indexb to indexc-1 to thesequencev.

Theoperator module also defines a few predicates to test thetype of objects.Note:Be careful not to misinterpret theresults of these functions; onlyisCallable() has anymeasure of reliability with instance objects. For example:

>>> class C:...     pass... >>> import operator>>> o = C()>>> operator.isMappingType(o)1

isCallable(o)
Deprecated since release 2.0.Use thecallable() built-in function instead.

Returns true if the objecto can be called like a function,otherwise it returns false. True is returned for functions, bound andunbound methods, class objects, and instance objects which support the__call__() method.

isMappingType(o)
Returns true if the objecto supports the mapping interface.This is true for dictionaries and all instance objects.Warning:There is no reliable way to test if an instancesupports the complete mapping protocol since the interface itself isill-defined. This makes this test less useful than it otherwise mightbe.

isNumberType(o)
Returns true if the objecto represents a number. This is truefor all numeric types implemented in C, and for all instance objects.Warning:There is no reliable way to test if an instancesupports the complete numeric interface since the interface itself isill-defined. This makes this test less useful than it otherwise mightbe.

isSequenceType(o)
Returns true if the objecto supports the sequence protocol.This returns true for all objects which define sequence methods in C,and for all instance objects.Warning:There is no reliableway to test if an instance supports the complete sequence interfacesince the interface itself is ill-defined. This makes this test lessuseful than it otherwise might be.

Example: Build a dictionary that maps the ordinals from0 to256 to their character equivalents.

>>> import operator>>> d = {}>>> keys = range(256)>>> vals = map(chr, keys)>>> map(operator.setitem, [d]*len(keys), keys, vals)


Subsections


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:3.9 UserStringUp:3. Python Runtime ServicesNext:3.10.1 Mapping Operators to
Release 2.2.3, documentation updated on 30 May 2003.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2026 Movatter.jp