An average function builtin? - Ideas - Discussions on Python.org
An average function builtin? Ideas stdlib , builtins zykron1 (Ahsan Ahmed) September 4, 2024, 4:37am 1 Originally started on github: math.average function proposal · Issue #123658 · python/cpython · GitHub I would like to propose a new builtin named avg that averages an itterable. I have made the code for this, however I am aware that this is a potentially controversial request. Perhaps this should be a part of the statistics module?
You can find a pr for this here: gh-123658 Added a built-in average function by zykron1 · Pull Request #123659 · python/cpython · GitHub ericvsmith (Eric V. Smith) September 4, 2024, 4:53am 2 There’s already statistics.mean() , so is this really just about making that a built in function? Personally I don’t think it’s used often enough to justify it becoming a built in. 14 Likes zykron1 (Ahsan Ahmed) September 4, 2024, 5:09am 3 I recognize your perspective on statistics.mean() is currently accessible.
But there are a few reasons why it could be advantageous to have average as a built-in function: Convenience: You don’t have to import extra modules to access built-in functionalities. This can make code simpler, especially for novices or short scripts where it’s preferable to have as few imports as possible. Consistency: For frequent operations like sum, min, and max, Python comes with a number of built-in functions.
This list would become consistent with other basic operations if average was added, which may improve the intuitiveness of Python’s standard library. Performance: Because a built-in function is implemented at a lower level, it is usually faster. Benchmarks, for example, have indicated that utilizing statistics could be up to 12 times slower than a built-in average function.mean(), that iscarried out using only Python.
When average computations are made frequently or on huge datasets, this speed increase may be substantial. Standard Library Philosophy: Python places a lot of emphasis on minimising boilerplate code and making the language easy to use. This idea is supported by the provision of avg as a built-in function, which eliminates the need to create extra code for a common operation. Although statistics.mean() is a helpful function, adding avg as a built-in could improve the functionality, speed, and accessibility of this fundamental statistical operation. storchaka (Serhiy Storchaka) September 4, 2024, 6:09am 4 Priorities.
Is having statistics.mean() as a builtin function more useful than having functools.reduce() as a builtin function? 5 Likes petersuter (Peter Suter) September 4, 2024, 7:07am 5 Have you read PEP 450 – Adding A Statistics Module To The Standard Library | peps.python.org ? Correctness over speed. It is easier to speed up a correct but slow function than to correct a fast but buggy one. There would be many hurdles: Do you handle all the special cases like NaN etc. the same way?
Have you tried improving the performance of statistics without adding builtins and without changing the behavior of such special cases? Did you compare to statistics.fmean ? Or to sum(x)/len(x) ? Why avg instead of average or mean ? … 2 Likes Rosuav (Chris Angelico) September 4, 2024, 8:22am 6 Be careful with this one, as it assumes you can iterate over it and get its length separately (or iterate over it twice and get the same number of elements), which isn’t true of arbitrary iterables.
But it does show why this isn’t nearly as important as the others, since for anything where that IS true (most likely a list), it’s by far the simplest solution. 2 Likes dg-pb (dg-pb) September 4, 2024, 8:52am 7 I have been looking into similar things for a while now. I have been trying to work on special cases similar to this one, but same as you encountered a lot of resistance and most of it is reasonable. See couple of my PRs that were somewhat similar to yours that were rejected: gh-122586: itertools.count.(peek, consume) by dg-pb · Pull Request #122588 · python/cpython · GitHub gh-120478: `itertools.ilen` addition by dg-pb · Pull Request #120483 · python/cpython · GitHub Numeric, iterator & numeric-iterator operations in Python could use some serious work.
And there are already enough of content to draw from to come up with more radical advancements. Thus, introducing more complexity with special cases that can already be achieved (even if via slower methods) is probably not a good way forward. E.g. sum already has performant and flexible approach to reduction via add . I think what would be more useful is developments of CAPI, which introduce intermediate iterator protocol that allows re-using correct and performant accumulators, reducers, key-predicate-signalling at C level.
Thus, ideally, if this was so, this specific case could re-use same (correct and fast) accumulation as builtins.sum with minimal amount of effort. Have a look at conclusion of Add `index: bool` keyword to `min`, `max`, `sorted` - #91 by dg-pb . This is where I am currently at with this. And I don’t think mean is that often used to be added to builtins. statistics.mean is a very reasonable place for it IMO. 1 Like Wombat (Zeke W) September 4, 2024, 11:22am 8 The recurring theme is that nothing written in plain python is fast enough.
Have you considered using rust? Wombat (Zeke W) September 4, 2024, 11:27am 9 When speed is paramount, use numpy. That is faster than a python builtin function could ever be. dg-pb (dg-pb) September 4, 2024, 11:45am 10 It does a different thing. pf_moore (Paul Moore) September 4, 2024, 11:53am 11 No, the recurring theme is that people are interested in speeding up Python primitives where possible. Because why wouldn’t we do that?
But most code written in Python is fast enough [1] . And most code that isn’t, can be made fast enough by language-agnostic optimisations like caching, coding to use the hardware effectively, and similar techniques. It’s a tiny minority for which optimising language primitives is critical - it’s often nice to have, but that’s entirely different. otherwise, why would so many people write code in Python? ↩︎ 6 Likes dg-pb (dg-pb) September 4, 2024, 11:53am 12 My interpretation of the situation is very much different.
Em… Yes. Have you? zykron1 (Ahsan Ahmed) September 4, 2024, 9:32pm 13 You mentioned prioritizing correctness over speed. My implementation achieves both—it’s faster and returns the exact same result, because it’s written in C as a built-in. While fmean is faster in some specific cases, it doesn’t consistently outperform a built-in. You also suggested using sum(x) / len(x) . While that’s close in speed to my implementation, it’s worth noting that len(x) will return 0 for an empty iterable, leading to a division by zero error—something my approach handles more gracefully.
As for why avg instead of average or mean , I’d counter with: why len instead of length ? Simplicity and brevity are often valued in Python’s naming conventions, and avg aligns with that. oscarbenjamin (Oscar Benjamin) September 4, 2024, 10:53pm 14 Be careful using the word “exact” here. Does your implementation always return the exact same result as the statistics.mean function for all possible inputs? What does it do in this case: >>> import statistics >>> statistics.mean([1e100, 3, -1e100]) 1.0 2 Likes zykron1 (Ahsan Ahmed) September 4, 2024, 11:48pm 16 Yes I tested with scientific numbers.
It does in fact work perfectly fine. Currently the code for this lives on my fork, however I will not attempt a merge until enough support. Python 3.14.0a0 (heads/fix-issue-123658-dirty:e5477316b4, Sep 4 2024, 16:43:22) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> avg([1e100, 3, -1e100]) 1.0 >>> ``` ncoghlan (Alyssa Coghlan) September 4, 2024, 11:51pm 17 Adding an optional C accelerator module for statistics would be a much less controversial change than proposing a new built-in.
10 Lik
The rest of this page is on the live site.