473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Using __mul__

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
Jul 18 '05 #1
2 4506
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
Jul 18 '05 #2
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

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
2
by: rawCoder | last post by:
Hi All, I have a *.cer file, a public key of some one and I want to encrypt some thing using this public key. Can someone point me to a sample code for Encrypting some file using...
1
by: Mike | last post by:
When trying to compile (using Visual Web Developer 2005 Express Beta; frameworkv2.0.50215 ) the source code below I get errors (listed below due to the use of ICallBackEventHandler. Ultimately I...
10
by: Christopher Benson-Manica | last post by:
Why can't I use a class destructor in a using declaration: using MyClass::~MyClass; ? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org ...
17
by: beliavsky | last post by:
Many of my C++ programs have the line using namespace std; but the "Accelerated C++" book of Koenig and Moo has many examples where the library names are included one at a time, for example ...
8
by: Petter Reinholdtsen | last post by:
I ran into a problem on HP-UX 11.00 the other day, where it refused to compile a program using 'using namespace std;' at the top. The reason seem to be that the compiler refuses to accept 'using...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
12
by: Gerard Flanagan | last post by:
Hello I'm pretty new to Python and was wondering why the 'Square' method in the following code doesn't work. It doesn't fail, just doesn't do anything ( at least, not what I'd like! ). Why...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.