[Tutor] operator and function call

Remco Gerlich scarblac@pino.selwerd.nl
Tue, 22 May 2001 11:01:34 +0200


On  0, Praveen Pathiyil <ppathiyi@cisco.com> wrote:
> What is the rationale in implementing some functionalities both as an operator and as a function call ?
> 
> E.g: 2 raised to the power of 3 can be computed as 
> 
> 2 ** 3 and as pow(2,3)
> 
> Similarly a - b and operator.sub(a,b)
> 
> Any specific reasons for such an approach ?

Useful in practice?

In expressions you want to be able to write it using the operator, because
it's shorter easier to read.

But sometimes you want to pass around functions to other functions, and
though you could always write sub out yourself (sub = lambda x, y: x-y), the
existing ones in operator are faster because they're in C. You may want to
subtract all elements in list M from list L, for instance: 
map(operator.sub, L, M).

Also, the builtin pow() takes an optional third argument that the operator
can't take (and that I've never used, but that's something else...).

Why there is both a builtin pow() and a math.pow() with slightly different
behavior is a mystery. It probably just turned out that way.

-- 
Remco Gerlich