On Mar 1, 2020, at 05:03, Alex Hall <alex.moj...@gmail.com> wrote:
> 
> Is there anyone who thinks it's acceptable to run `len()` on arbitrary 
> objects for an error message? Assuming 'no', then the length is only shown if 
> the type is exactly one of list, tuple, str, etc. where we know __len__ 
> exists and is safe. Alternatively, instead of checking the type, we could 
> check if `__len__` itself belongs to a safe set, if we want to include e.g. 
> subclasses of `list`.

IIRC, the new unpacking code still works like the old in that it special-cases 
list and tuple (where it can use the fast indexing API that just accesses the C 
array underneath), but for everything else it calls a function with iter(obj). 
If so, adding the length for list and tuple would be trivial, but adding it for 
other builtin types would not.

Is list and tuple good enough? It won’t help your example use, where it’s str, 
but I think for that case you don’t generally care that the string had 6 chars 
instead of 2; you already know the problem from it being a string in the first 
place. I suspect that almost every time the type is right but the length isn’t, 
the type is list, tuple, or a non-builtin type. How often do you accidentally 
unpack a length-6 dict where you wanted to unpack a length-2 dict?
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/2A2DSFIZDLSMJJ7F5X37BT44YN5RQLDX/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to