[Python-ideas] Sort statement

Chris Rebert cvrebert at gmail.com
Mon Jun 16 00:19:26 CEST 2008

On Sun, Jun 15, 2008 at 12:46 PM, Brandon Mintern <bmintern at gmail.com> wrote:
> On Sat, Jun 14, 2008 at 7:48 PM, Greg Ewing <greg.ewing at 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 at python.org
> http://mail.python.org/mailman/listinfo/python-ideas

More information about the Python-ideas mailing list