• Why isn't there a built-in product()?

    From Michael F. Stemper@21:1/5 to All on Thu Jul 7 13:26:51 2022
    sum() is wonderful.

    >>> nums = [1,2,3]
    >>> sum(nums)
    6
    >>> product(nums)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    NameError: name 'product' is not defined
    >>>

    I understand that there is no product() or prod(). Does anybody
    here know why that was not included in the language? It seems
    as if it would be useful, so there must have been some rationale
    for that decision.

    --
    Michael F. Stemper
    87.3% of all statistics are made up by the person giving them.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Michael F. Stemper on Thu Jul 7 18:59:17 2022
    Supersedes: <prod-20220707195304@ram.dialup.fu-berlin.de>
    [added ", 1"]

    "Michael F. Stemper" <michael.stemper@gmail.com> writes:
    sum() is wonderful.
    nums = [1,2,3]
    sum(nums)
    6
    product(nums)

    This is a contrived example. As such, it does not represent
    real-world statistics about the frequency of the need for
    "product" versus "sum".

    I understand that there is no product() or prod(). Does anybody
    here know why that was not included in the language? It seems
    as if it would be useful, so there must have been some rationale
    for that decision.

    Unlike "+", "sum" is not part of the languge proper,
    but of the standard library. Everything comes at a cost,
    so the frequency of its usage must outweigh the costs of
    its inclusion.

    product = lambda i: functools.reduce( operator.mul, i, 1 )

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Oscar Benjamin@21:1/5 to michael.stemper@gmail.com on Fri Jul 8 01:06:43 2022
    On Thu, 7 Jul 2022 at 22:55, Michael F. Stemper
    <michael.stemper@gmail.com> wrote:

    sum() is wonderful.

    >>> nums = [1,2,3]
    >>> sum(nums)
    6
    >>> product(nums)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    NameError: name 'product' is not defined
    >>>

    I understand that there is no product() or prod(). Does anybody
    here know why that was not included in the language? It seems
    as if it would be useful, so there must have been some rationale
    for that decision.

    There is math.prod:

    >>> from math import prod
    >>> prod([1, 2, 3, 4])
    24

    https://docs.python.org/3/library/math.html#math.prod

    --
    Oscar

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Michael F. Stemper@21:1/5 to Oscar Benjamin on Sat Jul 9 08:25:40 2022
    On 07/07/2022 19.06, Oscar Benjamin wrote:
    On Thu, 7 Jul 2022 at 22:55, Michael F. Stemper
    <michael.stemper@gmail.com> wrote:

    sum() is wonderful.

    I understand that there is no product() or prod(). Does anybody
    here know why that was not included in the language? It seems
    as if it would be useful, so there must have been some rationale
    for that decision.

    There is math.prod:

    >>> from math import prod
    >>> prod([1, 2, 3, 4])
    24

    https://docs.python.org/3/library/math.html#math.prod

    I did not know that. Thanks.


    --
    Michael F. Stemper
    Outside of a dog, a book is man's best friend.
    Inside of a dog, it's too dark to read.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)