I've also had to special case strings when dealing with iterables
generically, and it's annoying, but it's not a big deal. The real problem
is when you meant to pass an iterable of strings and you just passed a
single string and it produces confusing behaviour - something more subtle
than each character being laid out separately. And even this is not that
hard for experienced devs like us to figure out, but it really bites
beginners hard, and I think that's the argument worth focusing on.

A common example is when beginners write code like this:

    cursor.execute('INSERT INTO strings VALUES (?)', 'hello')

and get this confusing error:

sqlite3.ProgrammingError: Incorrect number of bindings supplied. The
current statement uses 1, and there are 5 supplied.

Finding questions about these is pretty easy, below are some examples:

https://stackoverflow.com/questions/54856759/sqlite3-programmingerror-incorrect-number-of-bindings-supplied-the-current-sta
https://stackoverflow.com/questions/16856647/sqlite3-programmingerror-incorrect-number-of-bindings-supplied-the-current-sta
https://stackoverflow.com/questions/6066681/python-sql-select-statement-from-a-list-variable
https://stackoverflow.com/questions/33768447/incorrect-number-of-bindings-supplied-python
https://stackoverflow.com/questions/35560106/incorrect-number-of-bindings-supplied
https://stackoverflow.com/questions/58786727/incorrect-number-of-bindings-supplied-the-current-statement-uses-1-and-there-a

So first of all, I think we should probably have a check in the sqlite3
library for passing a single string as a parameter.

But in the general case, it would be great if strings weren't iterable and
trying to iterate over them raised an exception with a helpful generic
message, like:

"Strings are not iterable - you cannot loop over them or treat them as a
collection. Perhaps you meant to use string.chars(), string.split(), or
string.splitlines()?"
_______________________________________________
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/NUEGGJN5E5SSFXZTW4QMSEKACVTUSQOV/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to