Add a "key" parameter to bisect* functions?

Discussion in 'Python' started by Giacomo Alzetta, Sep 12, 2012.

  1. 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?
    Giacomo Alzetta, Sep 12, 2012
    #1
    1. Advertising

  2. 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.
    Giacomo Alzetta, Sep 12, 2012
    #2
    1. Advertising

  3. Giacomo Alzetta

    Miki Tebeka Guest

    > I've just noticed that the bisect module lacks of the key parameter.
    > ...
    > Is there some reason behind this lack?

    See full discussion at http://bugs.python.org/issue4356. Guido said it's going in, however there's no time frame for it.
    Miki Tebeka, Sep 12, 2012
    #3
  4. Il giorno mercoledì 12 settembre 2012 18:05:10 UTC+2, Miki Tebeka ha scritto:
    > > I've just noticed that the bisect module lacks of the key parameter.

    >
    > > ...

    >
    > > Is there some reason behind this lack?

    >
    > 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.
    Giacomo Alzetta, Sep 12, 2012
    #4
  5. Giacomo Alzetta

    Miki Tebeka Guest

    > 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 :)
    Miki Tebeka, Sep 12, 2012
    #5
    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. Gary Robinson

    bisect uses __cmp__()?

    Gary Robinson, Jul 29, 2003, in forum: Python
    Replies:
    0
    Views:
    328
    Gary Robinson
    Jul 29, 2003
  2. Replies:
    0
    Views:
    327
  3. SA Trygubenko

    bisect and Queue modules in Python 2.4

    SA Trygubenko, Mar 16, 2006, in forum: Python
    Replies:
    2
    Views:
    736
    Alex Martelli
    Mar 16, 2006
  4. Paulo da Silva

    bisect on a list of lists

    Paulo da Silva, Mar 9, 2007, in forum: Python
    Replies:
    9
    Views:
    373
    Gabriel Genellina
    Mar 10, 2007
  5. Robert Bossy

    why not bisect options?

    Robert Bossy, Feb 29, 2008, in forum: Python
    Replies:
    7
    Views:
    277
    Robert Bossy
    Mar 4, 2008
Loading...

Share This Page