Re:Type hinting for Python
by theli0nheart on Tuesday January 06, 2015 @12:49PM
(#8845775)
I can think of one reason: `<` and `>` are comparison operators in Python, and they are easily overloaded with the `__gt__` and `__lt__` magic methods. So, if `List` was an instance of an object with those overridden magic methods, it's unclear what you actually want to do (i.e., `List<int` might evaluate to True, and then `True>` would lead to a syntax error). `[` and `]` have no such limitations.
[ Parent
| on HN ]
"`List<int` might evaluate to True, and then `True>` would lead to a syntax error"
You are confusing parsing and reduction. In most languages - and almost certainly in Python - these are separate steps. "Parsing it wrong leads to a syntax error" is a good thing - it means you're forced to parse it right. It would be worse if there were multiple syntactically-valid interpretations (which there may well be).
Here's the problem:
`a<b>c` is already valid python. `3<5>2` evaluates to true, because python allows operator overloading. Because the < and > operators can be overloaded, there is no guarantee that an object of type "class" will not have them (this would, I believe, require some metaclass hackery, but still).
So, given that `object<int>()` throws a type error in python and not a syntax error, you can't unambiguously parse that.
Yeah, that's for sure a problem. There are potential ways around it, but none of them great (that I see, at least).
"So, given that `object<int>()` throws a type error in python and not a syntax error, you can't unambiguously parse that."
Apparently in Python 2, it's not even an error.