4 ms·
The proposal for square brackets List[int] in generics is the weirdest thing. There is a standard un-surprising way to do this and it is angle brackets List<int
by loftsy 12y ago
The proposal for square brackets List[int] in generics is the weirdest thing. There is a standard un-surprising way to do this and it is angle brackets List<int>. What is the reason for forcing developers to use this jarring syntax?
- theli0nheart 12y agoI 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.
- dllthomas 12y ago"`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).
- zardeh 12y agoHere'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.
- dllthomas 12y agoYeah, 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.
- afandian 12y agoAngle-brackets may be familiar to C++, Java, C# and friends. But square-brackets are used by Scala (and maybe others).
- ajanuary 12y agoEiffel also uses square brackets.
- rdtsc 12y agoI think < > are pretty jarring. Especially reading 3 nested levels of it. [],() or {} I find are easier on the eyes in general.
- dezgeg 12y agoAngle brackets cause difficulties during parsing: since there is a '>>' operator, getting a type like 'List<List<int>>' to parse properly would require either a hack in the lexer or requiring a space between closing angle brackets (as in 'List<List<int> >') This is a well-known problem in C++; some of the parsing rules were changed in C++11: http://stackoverflow.com/questions/15785496/c-templates-angle-brackets-pitfall-what-is-the-c11-fix http://stackoverflow.com/questions/15785496/c-templates-angl...
- ceronman 12y agoNote that Guido's proposal doesn't add any syntax changes. The idea is not to modify the language but rather to have a unified format for defining type annotations. Function annotations have been there for many years now, and the only thing that will be added for now is some support in the standard library for the objects that will be used as type annotations.
- tel 12y agoYou assume this is standard, but it's merely a crib from other languages. Haskell, for a zany instance, just uses spaces: `List Int`.
- rayiner 12y agoAngle brackets suck.
- frowaway001 12y agoThere is pretty much no reason to use <> except "that's what C++ did, because they didn't have any other symbols to spare". If done correctly, (e. g. [] for types, () for values, and >/< for binary comparisons) this approach achieves a lot of consistency and simplicity.
- frowaway001 12y agoThere is pretty much no reason to use <> except "that's what C++ did, because they didn't have any other symbols to spare". If done correctly, (e. g. [] for types, () for values, and >/< for binary comparisons) this approach achieves a lot of consistency and simplicity.
- Retra 12y agoCalling that 'generics' is even weirder. What's so generic about a list of ints? That sounds pretty specific to me...
- frowaway001 12y agoThere is pretty much no reason to use <> except "that's what C++ did, because they didn't have any other symbols to spare". If done correctly, (e. g. [] for types, () for values, and >/< for binary comparisons) this approach achieves a lot of consistency and simplicity.
- sanderjd 12y agoThis was a bikeshed in the Rust community as well a pretty long time ago. They went with the `List<int>` style, which makes sense in light of C++ programmers being a large target audience of theirs, but that's far less a consideration in Python's case. I personally slightly prefer the square brackets, because it seems to imply "contains" because of its association with retrieving values from containers.