4 ms·
You can always allow it later, but deprecating such a "feature" is a pain. And subtle errors/outright abuse can happen with some of these automatic coercions, s
by guitarbill 8y ago
You can always allow it later, but deprecating such a "feature" is a pain. And subtle errors/outright abuse can happen with some of these automatic coercions, so Python tends to be a bit more conservative than other dynamic languages. The most (in)famous example being comparison (edit: not addition) of an integer and `None`. Allowed in Python 2, non-intuitive IMO, and responsible for a few bugs in its time. Disallowed in Python 3:
> TypeError: '<' not supported between instances of 'int' and 'NoneType'
- Sean1708 8y agoDo you mean comparison with None? Python 2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 1 + None Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'int' and 'NoneType' >>> 1 < None False vs Python 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 1 + None Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'int' and 'NoneType' >>> 1 < None Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: int() < NoneType()
- guitarbill 8y agodoh, you're absolutely right, cheers! edited the parent for posterity, too.
- zestyping 8y agoBeing able to compare all values was useful. I miss it. None being less than everything is particularly useful.
- sametmax 8y agoIt was also a huge source of bugs. I often give an exercice prompting a number with input(), then checking it to another number in the Python for beginers course. Most students don't call int() before comparing. In Python 3, they get an error, and learn to do so. In Python 2, it seems to work sometime, and if I don't catch it, they will get in trouble one day. Just make your code explicit, it's the sane thing to do anyway.