Using an operator as an object

Erik Max Francis max at alcyone.com
Sun Mar 2 21:22:20 EST 2003


Tony C wrote:

> Does Python have the ability to pass an operator (+,/,- etc) as an
> object (not a string), so I don't have to use an if for each operator
> , as in the first function below ?

No, but it has functions that mimic the behavior of the operators in the
(wait for it) operator module:

>>> import operator
>>> operator.add(2, 3)
5
>>> operator.add([1, 2], [3, 4])
[1, 2, 3, 4]

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ He who has a _why_ to live can bear with almost any _how_.
\__/ Friedrich Nietzsche
    WebVal / http://www.alcyone.com/pyos/webval/
 URL scanner, maintainer, and validator in Python.




More information about the Python-list mailing list