Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Using __mul__

Reply
Thread Tools

Using __mul__

 
 
Thomas Philips
Guest
Posts: n/a
 
      07-03-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
 
Reply With Quote
 
 
 
 
Sam Jervis
Guest
Posts: n/a
 
      07-03-2004
Thomas Philips wrote:
> 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


The answer is in your question. __mul__ is a valid method of type int,
but __mul__ on its own is an undefined name. The following code works:

reduce(int.__mul__, [1, 2, 3])

Sam
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      07-03-2004
Sam Jervis wrote:

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


[...]

> The answer is in your question. __mul__ is a valid method of type int,
> but __mul__ on its own is an undefined name. The following code works:
>
> reduce(int.__mul__, [1, 2, 3])


Or use operator.mul if you cannot guarantee that all numbers are integers:

>>> reduce(int.__mul__, [1.0, 2, 3])

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: descriptor '__mul__' requires a 'int' object but received a
'float'
>>> reduce(operator.mul, [1.0, 2, 3])

6.0

Peter

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
__mul__ vs __rmul__ (and others) priority for different classes dmitrey Python 1 12-11-2009 08:24 PM
Is __mul__ sufficient for operator '*'? Muhammad Alkarouri Python 7 10-26-2009 12:02 AM
Using '__mul__' within a class Gerard Flanagan Python 12 09-25-2005 02:28 PM
Using GetOleDbSchemaTable to get SQL Server Field Description - using pete ASP .Net 1 08-29-2003 10:50 AM
Re: MVP? Index error on nested element using System.xml but NOT using msxml??? William F. Robertson, Jr. ASP .Net 1 06-25-2003 08:08 PM



Advertisments