Using an operator as an object

Tony C cappy2112 at yahoo.com
Mon Mar 3 03:19:52 CET 2003


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 ?

def  docalc( operator, num1, num2):

	if( operator == "+") :
	      return( num1 + num2)

	if( operator == "-") :
	      return( num1 - num2)


What I'd like to do is this


def  docalc( operator, num1, num2):

	return (num1 operator num2 )


Of course- the correct type of object would have to be passed in.

thanks




More information about the Python-list mailing list