Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add list.sorted() #39428

Closed
rhettinger opened this issue Oct 18, 2003 · 6 comments
Closed

Add list.sorted() #39428

rhettinger opened this issue Oct 18, 2003 · 6 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@rhettinger
Copy link
Contributor

BPO 825814
Nosy @aleaxit, @rhettinger
Files
  • cs.diff: Patch 1 with docs, unittests, and annoucement
  • cs2.diff: Patch 2 avoids unnecessary copy
  • cs3.diff: Patch 3 with better argument handling
  • sorted.diff: Patch 4 implements class method list.sorted()
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2003-10-29.09:46:06.000>
    created_at = <Date 2003-10-18.00:34:53.000>
    labels = ['interpreter-core']
    title = 'Add list.sorted()'
    updated_at = <Date 2003-10-29.09:46:06.000>
    user = 'https://github.com/rhettinger'

    bugs.python.org fields:

    activity = <Date 2003-10-29.09:46:06.000>
    actor = 'aleax'
    assignee = 'none'
    closed = True
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2003-10-18.00:34:53.000>
    creator = 'rhettinger'
    dependencies = []
    files = ['5645', '5646', '5647', '5648']
    hgrepos = []
    issue_num = 825814
    keywords = ['patch']
    message_count = 6.0
    messages = ['44800', '44801', '44802', '44803', '44804', '44805']
    nosy_count = 2.0
    nosy_names = ['aleax', 'rhettinger']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue825814'
    versions = ['Python 2.4']

    @rhettinger
    Copy link
    Contributor Author

    As discussed on py-dev, this returns a sorted copy of a
    list while leaving the original intact.

    @rhettinger rhettinger added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Oct 18, 2003
    @rhettinger rhettinger added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Oct 18, 2003
    @rhettinger
    Copy link
    Contributor Author

    Logged In: YES
    user_id=80475

    Revised the patch to avoid an unnecessary copy whenever the
    underlying object is not referenced anywhere else -- this
    occurs in the common use case: for elem in
    d.keys().copysort(): . . .

    @rhettinger
    Copy link
    Contributor Author

    Logged In: YES
    user_id=80475

    Rationale for the change
    ---------------------
    An inline sort is usable anywhere an expression is allowed.
    This includes important places like function call arguments
    and list comprehensions:

      todo = [t for t in tasks.copysort() if due_today(t)]
    
      genhistory(date, events.copysort(key=incidenttime))

    Spreading these out over multiple lines is an unnecessary
    distractor from the problem domain, making the code harder
    to understand and maintain.

    Also, using copysort() eliminates a unnecessary variable
    that changes state from unsorted to sorted and has a
    lifetime longer than the data is actually needed. In longer
    code fragments, this decreases code complexity, code length,
    the number of variables, and decreases the risk of using a
    variable in the wrong state which is a common source of
    programming errors.

    @rhettinger
    Copy link
    Contributor Author

    Logged In: YES
    user_id=80475

    Alex, can you give this patch a second review.

    I did not put in additional classmethod warnings because all
    plausible misunderstandings raise an error anyway.

    @rhettinger
    Copy link
    Contributor Author

    Logged In: YES
    user_id=80475

    Applied.

    @aleaxit
    Copy link
    Contributor

    aleaxit commented Oct 29, 2003

    Logged In: YES
    user_id=60314

    Looks good to me! I understand not wanting -- given the clean and
    spare code for list.sorted -- to go to the considerable trouble needed
    to give more specific error messages; however, if we release like this
    we WILL get questions about it on c.l.py and help@python.org -- "why doesn't ''for x in mydict.keys().sorted():'' work?!" etc. Do you mind if
    eventually I _do_ try to ensure the error messages specifically mention list.sorted being a classmethod and requiring one iterable argument?

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 9, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs)
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants