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
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
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
On Sat, Jun 14, 2008 at 7:48 PM, Greg Ewing <greg.ewing@canterbury.ac.nz> 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
On Sun, Jun 15, 2008 at 12:46 PM, Brandon Mintern <bmintern@gmail.com> wrote:
On Sat, Jun 14, 2008 at 7:48 PM, Greg Ewing <greg.ewing@canterbury.ac.nz> 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@python.org http://mail.python.org/mailman/listinfo/python-ideas
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@scottdial.com scodial@cs.indiana.edu
participants (6)
-
Brandon Mintern
-
Chris Rebert
-
David Pokorny
-
Greg Ewing
-
Scott Dial
-
Talin