Using __mul__

Discussion in 'Python' started by Thomas Philips, Jul 3, 2004.

  1. 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
    Thomas Philips, Jul 3, 2004
    #1
    1. Advertising

  2. Thomas Philips

    Sam Jervis Guest

    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
    Sam Jervis, Jul 3, 2004
    #2
    1. Advertising

  3. Thomas Philips

    Peter Otten Guest

    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
    Peter Otten, Jul 3, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William F. Robertson, Jr.
    Replies:
    1
    Views:
    972
    Kathy Burke
    Jun 25, 2003
  2. pete
    Replies:
    1
    Views:
    2,936
    Jon Yates
    Aug 29, 2003
  3. Gerard Flanagan

    Using '__mul__' within a class

    Gerard Flanagan, Sep 24, 2005, in forum: Python
    Replies:
    12
    Views:
    707
    John J. Lee
    Sep 25, 2005
  4. Muhammad Alkarouri

    Is __mul__ sufficient for operator '*'?

    Muhammad Alkarouri, Oct 20, 2009, in forum: Python
    Replies:
    7
    Views:
    422
    Mick Krippendorf
    Oct 26, 2009
  5. dmitrey
    Replies:
    1
    Views:
    936
    Terry Reedy
    Dec 11, 2009
Loading...

Share This Page