faqts : Computers : Programming : Languages : Python : Language and Syntax

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 7 people (100%) answered Yes
Recently 5 of 5 people (100%) answered Yes

Entry

Is there some way to refer to the and * operators as functions?

Nov 6th, 2002 08:41
Allan Caetano, unknown unknown, Alex, Nathan Froyd


Yeah, use the operator module

>>> import operator 
>>> operator.add (5, 2)
7
>>> operator.mul (5, 2)
10
>>> 

example::

If I want to have the length of a
vector v writing

reduce(lambda x,y:x+y,map(lambda x:x*x,v))

is not really nice. Instead I can write
something like:

reduce(operator.add, map(operator.mul, v, v))