class itemgetter(__builtin__.object)
| itemgetter(item, ...) --> itemgetter object
|
| Return a callable object that fetches the given item(s) from its operand.
| After, f=itemgetter(2), the call f(r) returns r[2].
| After, g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3])
相当于
def itemgetter(i,*a):
def func(obj):
r = obj[i]
if a:
r = (r,) + tuple(obj[i] for i in a)
return r
return func
>>> a = [1,2,3]
>>> b=operator.itemgetter(1)
>>> b(a)
2
>>> b=operator.itemgetter(1,0)
>>> b(a)
(2, 1)
>>> b=itemgetter(1)
>>> b(a)
2
>>> b=itemgetter(1,0)
>>> b(a)
(2, 1)
| itemgetter(item, ...) --> itemgetter object
|
| Return a callable object that fetches the given item(s) from its operand.
| After, f=itemgetter(2), the call f(r) returns r[2].
| After, g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3])
相当于
def itemgetter(i,*a):
def func(obj):
r = obj[i]
if a:
r = (r,) + tuple(obj[i] for i in a)
return r
return func
>>> a = [1,2,3]
>>> b=operator.itemgetter(1)
>>> b(a)
2
>>> b=operator.itemgetter(1,0)
>>> b(a)
(2, 1)
>>> b=itemgetter(1)
>>> b(a)
2
>>> b=itemgetter(1,0)
>>> b(a)
(2, 1)