Add a "key" parameter to bisect* functions?

G

Giacomo Alzetta

I've just noticed that the bisect module lacks of the key parameter.

The documentation points to a recipe that could be used to handle a sorted collection, but I think it's an overkill if I want to bisect my sequence only once or twice with a key. Having something like `bisect(sequence, key=my_key)` would be much easier and would conform to the other operations such as max/min/sorted.

Is there some reason behind this lack?
 
G

Giacomo Alzetta

Il giorno mercoledì 12 settembre 2012 17:54:31 UTC+2, Giacomo Alzetta ha scritto:
I've just noticed that the bisect module lacks of the key parameter.



The documentation points to a recipe that could be used to handle a sorted collection, but I think it's an overkill if I want to bisect my sequence only once or twice with a key. Having something like `bisect(sequence, key=my_key)` would be much easier and would conform to the other operations such as max/min/sorted.



Is there some reason behind this lack?

Uhm, I've found this piece of documentation: http://docs.python.org/library/bisect.html#other-examples

Now, what it's stated there does not make sense for me:

1) Adding a key/reversed parameter wont decrease the speed of calls to bisect that does not use those parameters. At least a good implementation should guarantee that

2) Yes, providing a key means that either bisect should do some caching in order to speed multiple look-ups, or the keys have to be recomputed every time.
But, I think that using this as a reason to not provide this parameter is wrong.
It's up to the user to decide what has to be done to make code fast.
If he has to do multiple lookups then he should precompute them(but this istrue also for sort etc.), but if he just has to use this feature once thancomputing "on-the-fly" is simply perfect.

3) Also, the fact that you can bisect only in asceding order sounds strangeto me. It's not hard to bisect in both directions...

Probably it would be correct to document possible pitfalls of using the eventually added "key" and "reversed" parameters(such as multiple key evaluation etc.), but I can't see other cons to this.

Also, this change would be 100% backward compatible.
 
G

Giacomo Alzetta

Il giorno mercoledì 12 settembre 2012 18:05:10 UTC+2, Miki Tebeka ha scritto:
See full discussion at http://bugs.python.org/issue4356. Guido said it's going in, however there's no time frame for it.

Oh, nice. So eventually it'll be added.

Correct me if I'm wrong, but the reason for this absence is that people could think that doing repeated bisects using keys would be fast, even though keys has to be recomputed?

I think that could simply be fixed with a "Note:\Warning:" on the documentation.
So that you can create keys with caching or use precomputed keys when you have to do a lot of bisects.
 
M

Miki Tebeka

Correct me if I'm wrong, but the reason for this absence is that people could
think that doing repeated bisects using keys would be fast, even though keys
has to be recomputed?
I think the main point Raymond had was:
If we added key= to bisect, it would encourage bad design and steer
people after from better solutions.

But you need to ask him :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top