[Python-ideas] Shuffled
Steven D'Aprano
steve at pearwood.info
Wed Sep 7 22:31:38 EDT 2016
On Thu, Sep 08, 2016 at 12:22:28AM +0200, Sven R. Kunze wrote:
> About the confusion of returning None. It's not confusing in the sense
> of "o my god, I did it wrong, I need to learn it", but more, like "I
> used shuffle, BECAUSE it's the only one I could find in a hurry"
Then don't be in such a hurry.
Honestly, there is no substitute for professionalism. Outside of fantasy
novels and movies, there has never been a "ticking time bomb" situation
where a programmer has to write code to shuffle a list in six seconds or
the world will be destroyed. There is *always* time to at least glance
at the docs, and it is never more than a handful of keypresses away in
the interactive interpreter:
help(random.shuffle)
And if you don't read the docs? Oh well, you find out soon enough once
you actually test your code or have a code review. No harm done.
If the programmer doesn't do *any* of those things -- doesn't read the
docs, doesn't test their code, doesn't do code reviews -- then there's
no hope for them. We could provide shuffled() and shuffle() and they'll
still end up picking the wrong one fifty percent of the time.
"My wild guess of how a function works was wrong, so now I'm cheesed off
that the function doesn't do what I expect" is NOT a good reason to add
things to the standard library.
> I would expect this from
> Python as it already provides both alternatives for sorting".
Does that mean you expect Python to provide appended() as well as
append(), extended() as well as extend(), popped() as well as pop(),
cleared() as well as clear(), inserted() as well as insert(), removed()
as well as remove()?
If your response is to say "Why do we need all those? Why on earth would
anyone need to use list.cleared()?" then perhaps you can now understand
why shuffled() has to prove itself too. Functions don't get added just
because they *can* be added. They have to be worth it. Every function
has costs as well as benefits. Each function adds:
- more code to download, compile, maintain
- more tests
- more documentation
- more for the user to learn
- more choices for the user to decide between
- more complexity
- more risk of bugs
Unless the proposal can demonstrate that the benefit outweighs the
costs, it should not be added. Merely stating that you need it
doesn't demonstate that a four-line helper function at the top of
your module isn't sufficient.
For what's it is worth, if it were *my* call, I'd accept that the costs
of adding this are low, but the benefits are just a *tiny* bit higher.
But that's a judgement call, and if Raymond see the cost:benefit ratio
going the other way, I have no objective argument to change his mind.
--
Steve
More information about the Python-ideas
mailing list