FAQ
In the spirit of going in the reverse direction of turning print into
a function, what does python-ideas think of the sort statement?

numlist = [2,5,4]
sort numlist
sort numlist asc # borrowed from SQL ORDER BY statement
sort numlist desc
sort by employee.last_name for employee in employee_list # this uses key sorting

The main advantage is that it is impossible to make this mistake:

x = y.sort()

when you really mean

x = sorted(y)

Cheers,
David

Search Discussions

  • Talin at Jun 14, 2008 at 7:43 pm

    David Pokorny wrote:
    In the spirit of going in the reverse direction of turning print into
    a function, what does python-ideas think of the sort statement?
    And here I was trying so hard to forget everything I ever learned about
    COBOL programming...

    http://www.cs.niu.edu/~abyrnes/csci465/notes/465sort.htm

    -- Talin
  • Greg Ewing at Jun 14, 2008 at 11:48 pm

    David Pokorny wrote:
    In the spirit of going in the reverse direction of turning print into
    a function, what does python-ideas think of the sort statement?
    I don't think that sorting is a frequent enough operation
    in general to justify having its own statement.
    The main advantage is that it is impossible to make this mistake:

    x = y.sort()
    If you make that mistake, you find out about it very
    quickly, and you learn not to make it again.

    Also, there are many other methods that have the same
    characteristic. Would you want to turn all of them into
    statements as well?

    --
    Greg
  • Brandon Mintern at Jun 15, 2008 at 7:46 pm

    On Sat, Jun 14, 2008 at 7:48 PM, Greg Ewing wrote:
    I don't think that sorting is a frequent enough operation
    in general to justify having its own statement.
    Agreed.
    The main advantage is that it is impossible to make this mistake:

    x = y.sort()
    If you make that mistake, you find out about it very
    quickly, and you learn not to make it again.
    Yes, but having .sort() return self would also solve this problem
    without anything as radical as introducing a new keyword and syntax.
    I'm not saying that this should be done, but I think this would be a
    much better alternative than the proposed sort syntax.

    Brandon
  • Chris Rebert at Jun 15, 2008 at 10:19 pm

    On Sun, Jun 15, 2008 at 12:46 PM, Brandon Mintern wrote:
    On Sat, Jun 14, 2008 at 7:48 PM, Greg Ewing wrote:
    I don't think that sorting is a frequent enough operation
    in general to justify having its own statement.
    Agreed.
    The main advantage is that it is impossible to make this mistake:

    x = y.sort()
    If you make that mistake, you find out about it very
    quickly, and you learn not to make it again.
    Yes, but having .sort() return self would also solve this problem
    without anything as radical as introducing a new keyword and syntax.
    I'm not saying that this should be done, but I think this would be a
    much better alternative than the proposed sort syntax.
    But that would be more confusing and make it seem to the newbie that
    .sort() returns a *new* sorted list rather than sorting the list
    in-place. Returning None (or not returning anything, which has the
    same effect) is idiomatic in Python to indicate a method is a mutator.
    And they'll quickly get a "TypeError: unsubscriptable object" and
    learn this lesson if they use list.sort() incorrectly.

    Although I admit, that error message could be improved. At least
    including the object in question would be better, for instance:
    TypeError: unsubscriptable object "None"
    Or perhaps also changing "unsubscriptable" to something more
    comprehensible to newbies:
    TypeError: object "None" does not support the subscript operator


    - Chris R.
    Brandon
    _______________________________________________
    Python-ideas mailing list
    Python-ideas at python.org
    http://mail.python.org/mailman/listinfo/python-ideas
  • Scott Dial at Jun 16, 2008 at 2:26 am

    Chris Rebert wrote:
    Although I admit, that error message could be improved. At least
    including the object in question would be better, for instance:
    TypeError: unsubscriptable object "None"
    Or perhaps also changing "unsubscriptable" to something more
    comprehensible to newbies:
    TypeError: object "None" does not support the subscript operator
    This error message was discussed on python-dev back in April, but I
    don't know that anything ever came from it.

    http://mail.python.org/pipermail/python-dev/2008-April/078744.html

    It would good if it was at least unified for all objects (which it was
    not at the time, maybe it is now..)

    -Scott

    --
    Scott Dial
    scott at scottdial.com
    scodial at cs.indiana.edu

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-ideas @
categoriespython
postedJun 14, '08 at 4:52a
activeJun 16, '08 at 2:26a
posts6
users6
websitepython.org

People

Translate

site design / logo © 2018 Grokbase