Using __mul__

Thomas Philips tkpmep at hotmail.com
Sat Jul 3 09:59:43 EDT 2004


To compute the product of a list of numbers, I tried entering
>>>reduce(__mul__,[1,2,3])

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in -toplevel-
    reduce(__mul__,[1,2,3])
NameError: name '__mul__' is not defined

I can get the answer I want by defining a function that returns the
product of two numbers:
>>> def product(x,y):
	return x*y

and then using it:
>>> reduce(product,[1,2,3])
6

But why does my first approach not work? __mul__ is a valid method of
type int. Is there an obvious flaw in my logic?

Thomas Philips



More information about the Python-list mailing list