Using an operator to add in Python

前端 未结 5 2168
隐瞒了意图╮
隐瞒了意图╮ 2021-01-06 13:35

Consider:

operator.add(a, b)

I\'m having trouble understanding what this does. An operator is something like +-*/, so what doe

5条回答
  •  天命终不由人
    2021-01-06 13:46

    As operator.add is a function and you can pass argument to it, it's for the situations where you can not use statements like a+d, like the map or itertools.imap functions. For better understanding, see the following example:

    >>> import operator
    >>> from itertools import imap
    >>> list(imap(operator.add,[1,3],[5,5]))
    [6, 8]
    

提交回复
热议问题