[Python-ideas] Unhelpful error message from sorted

Michael Foord fuzzyman at gmail.com
Tue May 15 11:01:21 CEST 2012


On 15 May 2012 09:02, Steven D'Aprano <steve at pearwood.info> wrote:

> On Tue, May 15, 2012 at 12:28:02AM +0100, Michael Foord wrote:
> > Hello all,
> >
> > It seems to me that the following error message, whilst technically
> > correct, is unhelpful:
> >
> > >>> sorted([3, 2, 1], reverse=None)
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> > TypeError: an integer is required
>
> I don't know what you mean by "technically correct". Surely the Pythonic
> idiom is to allow any value in a boolean context.
>
> sorted() here is neither one thing nor the other, neither duck-typing,
> since it won't accept flags that quack like a bool, nor does it strictly
> insist on a bool, since it accepts ints:
>
> >>> sorted([1,2,3], reverse=42)
> [3, 2, 1]
>
> I can't see any sense to this almost-but-not-quite type restriction.
>
> +1 to allow any object that is truthy or falsey (i.e. anything).
>

I would rather have "sorted(some_list, reverse=[1, 2, 3])" raise an error
(and preferably a helpful error message that tells you which argument is
faulty and why).

Michael


> +0 to allowing only True or False.
> -1 to half-heartedly allowing ints but no other values.
>
>
> --
> Steven
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>



-- 

http://www.voidspace.org.uk/

May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120515/787bdb59/attachment.html>


More information about the Python-ideas mailing list