python的operator模块_operator模块-PYTHON

全部列表

>>> import operator

>>> dir(operator)

['__abs__', '__add__', '__and__', '__concat__', '__contains__', '__delitem__',

'__delslice__', '__div__', '__doc__', '__eq__', '__floordiv__', '__ge__',

'__getitem__', '__getslice__', '__gt__', '__iadd__', '__iand__', '__iconcat__',

'__idiv__', '__ifloordiv__', '__ilshift__', '__imod__', '__imul__', '__index__',

'__inv__', '__invert__', '__ior__', '__ipow__', '__irepeat__', '__irshift__',

'__isub__', '__itruediv__', '__ixor__', '__le__', '__lshift__', '__lt__', '__mod__',

'__mul__', '__name__', '__ne__', '__neg__', '__not__', '__or__', '__package__',

'__pos__', '__pow__', '__repeat__', '__rshift__', '__setitem__', '__setslice__',

'__sub__', '__truediv__', '__xor__', 'abs', 'add', 'and_', 'attrgetter', 'concat',

'contains', 'countOf', 'delitem', 'delslice', 'div', 'eq', 'floordiv', 'ge',

'getitem', 'getslice', 'gt', 'iadd', 'iand', 'iconcat', 'idiv', 'ifloordiv',

'ilshift', 'imod', 'imul', 'index', 'indexOf', 'inv', 'invert', 'ior', 'ipow',

'irepeat', 'irshift', 'isCallable', 'isMappingType', 'isNumberType', 'isSequenceType',

'is_', 'is_not', 'isub', 'itemgetter', 'itruediv', 'ixor', 'le', 'lshift', 'lt',

'methodcaller', 'mod', 'mul', 'ne', 'neg', 'not_', 'or_', 'pos', 'pow', 'repeat',

'rshift', 'sequenceIncludes', 'setitem', 'setslice', 'sub', 'truediv', 'truth', 'xor']

操作符和函数的对应关系

Operation

Syntax

Function

Addition

a + b

add(a, b)

Concatenation

seq1 + seq2

concat(seq1, seq2)

Containment Test

obj in seq

contains(seq, obj)

Division

a / b

div(a, b) (without

__future__.division)

Division

a / b

truediv(a, b) (with

__future__.division)

Division

a // b

floordiv(a, b)

Bitwise And

a & b

and_(a, b)

Bitwise Exclusive Or

a ^ b

xor(a, b)

Bitwise Inversion

~ a

invert(a)

Bitwise Or

a | b

or_(a, b)

Exponentiation

a ** b

pow(a, b)

Identity

a is b

is_(a, b)

Identity

a is not b

is_not(a, b)

Indexed Assignment

obj[k] = v

setitem(obj, k, v)

Indexed Deletion

del obj[k]

delitem(obj, k)

Indexing

obj[k]

getitem(obj, k)

Left Shift

a << b

lshift(a, b)

Modulo

a % b

mod(a, b)

Multiplication

a * b

mul(a, b)

Negation (Arithmetic)

- a

neg(a)

Negation (Logical)

not a

not_(a)

Positive

+ a

pos(a)

Right Shift

a >> b

rshift(a, b)

Sequence Repetition

seq * i

repeat(seq, i)

Slice Assignment

seq[i:j] = values

setitem(seq, slice(i, j), values)

Slice Deletion

del seq[i:j]

delitem(seq, slice(i, j))

Slicing

seq[i:j]

getitem(seq, slice(i, j))

String Formatting

s % obj

mod(s, obj)

Subtraction

a - b

sub(a, b)

Truth Test

obj

truth(obj)

Ordering

a < b

lt(a, b)

Ordering

a <= b

le(a, b)

Equality

a == b

eq(a, b)

Difference

a != b

ne(a, b)

Ordering

a >= b

ge(a, b)

Ordering

a > b

gt(a, b)

类itemgetter

构造函数

itemgetter(item, ...) --> itemgetter object

其返回一个callable的对象,其作用是返回操作数的第n个元素

f = itemgetter(2), f(r) 就是返回r[2]

g = itemgetter(2, 5, 3), g(r) 就是返回(r[2], r[5], r[3])

__call__()

x.__call__(...) == x(...)

__getattribute__()

x.__getattribute__('name') == x.name

__new__()

T.__new__(S, ...) -> a new object with type S, a subtype of T

类attrgetter

构造函数

attrgetter(attr, ...) --> attrgetter object

其返回一个callable的对象,其作用是返回操作数某个属性

f = attrgetter('name'), f(r) 就是返回r.name

g = attrgetter('name', 'date'), g(r) 就是返回(r.name, r.date)

h = attrgetter('name.first', 'name.last'), h(r)就是返回(r.name.first, r.name.last)

__call__()

x.__call__(...) == x(...)

__getattribute__()

x.__getattribute__('name') == x.name

__new__()

T.__new__(S, ...) -> a new object with type S, a subtype of T

类methodcaller

构造函数

methodcaller(name, ...) --> methodcaller object

其返回一个callable的对象,其作用是调用指定函数

f = methodcaller('name'), f(r) 就是执行r.name()

g = methodcaller('name', 'date', foo=1), g(r) 就是执行r.name('date', foo=1)

__call__()

x.__call__(...) == x(...)

__getattribute__()

x.__getattribute__('name') == x.name

__new__()

T.__new__(S, ...) -> a new object with type S, a subtype of T

__abs__(),abs()

abs(a) -- Same as abs(a)

>>> operator.abs(-12)

12

>>> operator.__abs__(-12)

12

__index__(),index()

index(a) -- Same as a.__index__()

__inv__(),inv()

inv(a) -- Same as ~a

__invert__(),invert()

invert(a) -- Same as ~a

位运算

__xor__(),xor()

xor(a, b) -- Same as a ^ b

__ixor__(),ixor()

a = ixor(a, b) -- Same as a ^= b

__or__(),or_()

or_(a, b) -- Same as a | b

__ior__(),ior()

a = ior(a, b) -- Same as a |= b

__and__(),and_()

and_(a, b) -- Same as a & b

__iand__(),iand()

a = iand(a, b) -- Same as a &= b

__not__(),not_()

not_(a) -- Same as not a

比较运算

__eq__(),eq()

eq(a, b) -- Same as a==b

__ne__(),ne()

ne(a, b) -- Same as a!=b

__lt__(),lt()

lt(a, b) -- Same as a

__le__(),le()

le(a, b) -- Same as a<=b

__gt__(),gt()

gt(a, b) -- Same as a>b

__ge__(),ge()

ge(a, b) -- Same as a>=b

移位运算

__lshift__(),lshift()

lshift(a, b) -- Same as a << b

__ilshift__(),ilshift()

a = ilshift(a, b) -- Same as a <<= b

__rshift__(),rshift()

rshift(a, b) -- Same as a >> b

__irshift__(),irshift()

a = irshift(a, b) -- Same as a >>= b

数学运算

__add__(),add()

add(a, b) -- Same as a + b

__iadd__(),iadd()

a = iadd(a, b) -- Same as a += b

__sub__(),sub()

sub(a, b) -- Same as a - b

__isub__(),isub()

a = isub(a, b) -- Same as a -= b

__mul__(),mul()

mul(a, b) -- Same as a * b

__imul__(),imul()

a = imul(a, b) -- Same as a *= b

__div__(),div()

div(a, b) -- Same as a / b when __future__.division is not in effect

__idiv__(),idiv()

a = idiv(a, b) -- Same as a /= b when __future__.division is not in effect

__floordiv__(),floordiv()

floordiv(a, b) -- Same as a // b

__ifloordiv__(),ifloordiv()

a = ifloordiv(a, b) -- Same as a //= b

__truediv__(),truediv()

truediv(a, b) -- Same as a / b when __future__.division is in effect

__itruediv__(),itruediv()

a = itruediv(a, b) -- Same as a /= b when __future__.division is in effect

__mod__(),mod()

mod(a, b) -- Same as a % b

__imod__(),imod()

a = imod(a, b) -- Same as a %= b

__pos__(),pos()

pos(a) -- Same as +a

__neg__(),neg()

neg(a) -- Same as -a

__pow__(),pow()

pow(a, b) -- Same as a ** b

__ipow__(),ipow()

a = ipow(a, b) -- Same as a **= b

列表操作

列表相连__concat__(),concat()

concat(a, b) -- Same as a + b, for a and b sequences

>>> a=[1,2,3]

>>> b=[4,5,6]

>>> operator.concat(a,b)

[1, 2, 3, 4, 5, 6]

>>> operator.__concat__(a,b)

[1, 2, 3, 4, 5, 6]

__iconcat__(),iconcat()

a = iconcat(a, b) -- Same as a += b, for a and b sequences

__repeat__(),repeat()

repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer

__irepeat__(),irepeat()

a = irepeat(a, b) -- Same as a *= b, where a is a sequence, and b is an integer.

__getitem__(),getitem()

getitem(a, b) -- Same as a[b]

__setitem__(),setitem()

setitem(a, b, c) -- Same as a[b] = c

__delitem__()

delitem(a, b) -- Same as del a[b]

注意:这个是对原数据的操作

>>> a=[1, 2, 3]

>>> operator.__delitem__(a, 2)

>>> a

[1, 2]

>>> a=[1, 2, 3]

>>> operator.delitem(a, 2)

>>> a

[1, 2]

__getslice__(),getslice()

getslice(a, b, c) -- Same as a[b:c]

__setslice__(),setslice()

setslice(a, b, c, d) -- Same as a[b:c] = d

__delslice__(),delslice()

delslice(a, b, c) -- Same as del a[b:c]

sequenceIncludes()

sequenceIncludes(a, b) -- Same as b in a (note reversed operands; deprecated)

__contains__(),contains()

contains(a, b) -- Same as b in a (note reversed operands)

>>> a

[1, 2, 3]

>>> operator.contains(a,1)

True

>>> operator.__contains__(a,1)

True

countOf()

countOf(a, b):Return the number of times b occurs in a

类型判断

isCallable()

isCallable(a) -- Same as callable(a)

isMappingType()

isMappingType(a) -- Return True if a has a mapping type, False otherwise

isNumberType()

isNumberType(a) -- Return True if a has a numeric type, False otherwise

isSequenceType()

isSequenceType(a) -- Return True if a has a sequence type, False otherwise

is_()

is_(a, b) -- Same as a is b

is_not()

is_not(a, b) -- Same as a is not b

>>>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值