Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3.6 vs 3.5 TypeError message on string concatenation

'Hello ' + 1 doesn't return the same error message on Python 3.5 and 3.6:

  • Python 3.5.2: TypeError: Can't convert 'int' object to str implicitly
  • Python 3.6.0: TypeError: must be str, not int

Is it a simple change in wording or is there something more subtle behind?

like image 625
Antoine Dusséaux Avatar asked Dec 29 '16 23:12

Antoine Dusséaux


People also ask

What was introduced in Python 3.6 that changed string formatting?

Python f-strings or formatted strings are the new way to format strings. This feature was introduced in Python 3.6 under PEP-498. It's also called literal string interpolation.

When did Python 3.6 release?

Python 3.6 was released on December 23, 2016. See the changelog for a full list of changes.

When was Python F string released?

Python 3 was released in 2008 which included an alternatives to formatting strings: str. format() . Fast forward to 2016, Python 3.6 formatted string literals (or f-strings) were released.


1 Answers

It was just some cleaning up of code that involved string objects. It also introduced some changes in the error messages when incompatible objects are used to make them a bit "more informative".

See: Issue 26057 - Avoid nonneeded use of PyUnicode_FromObject() that introduced this change if you're interested.

There's nothing subtle here, it is still illegal and all, the author changed the error message to what he considered a bit clearer.

Edit: I created Issue 29116 - Make str and bytes error messages on concatenation conform with other sequences to address this specific message while also addressing the error message for bytes types that have had a similarly verbose response when we do silly things with them:

>>> b'' + ''
TypeError: can't concat bytes to str
like image 51
Dimitris Fasarakis Hilliard Avatar answered Nov 16 '22 02:11

Dimitris Fasarakis Hilliard