News for hackers. Stuff that matters.
Re:Type hinting for Python by loftsy on Tuesday January 06, 2015 @12:46PM (#8845741)
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?
Re:Type hinting for Python by dezgeg on Tuesday January 06, 2015 @12:59PM (#8845862)
Angle 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-angl...

Re:Type hinting for Python by afandian on Tuesday January 06, 2015 @12:50PM (#8845782)
Angle-brackets may be familiar to C++, Java, C# and friends. But square-brackets are used by Scala (and maybe others).
Re:Type hinting for Python by ajanuary on Tuesday January 06, 2015 @01:00PM (#8845874)
Eiffel also uses square brackets.
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.
Re:Type hinting for Python by dllthomas on Tuesday January 06, 2015 @03:12PM (#8846607)
"`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).

Re:Type hinting for Python by zardeh on Wednesday January 07, 2015 @02:43AM (#8849335)
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.

Re:Type hinting for Python by dllthomas on Wednesday January 07, 2015 @11:36AM (#8851464)
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.

Re:Type hinting for Python by rdtsc on Tuesday January 06, 2015 @12:56PM (#8845835)
I think < > are pretty jarring. Especially reading 3 nested levels of it. [],() or {} I find are easier on the eyes in general.
Re:Type hinting for Python by ceronman on Tuesday January 06, 2015 @01:30PM (#8846091)
Note 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.
Re:Type hinting for Python by tel on Tuesday January 06, 2015 @02:16PM (#8846323)
You assume this is standard, but it's merely a crib from other languages. Haskell, for a zany instance, just uses spaces: `List Int`.
Re:Type hinting for Python by dllthomas on Tuesday January 06, 2015 @04:47PM (#8847341)
So does ML (albeit postfix), which came before Haskell or C++, right?
Re:Type hinting for Python by tel on Tuesday January 06, 2015 @05:27PM (#8847530)
Definitely Haskell, probably C++
Re:Type hinting for Python by sanderjd on Wednesday January 07, 2015 @01:29PM (#8852249)
This 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.
Re:Type hinting for Python by Retra on Tuesday January 06, 2015 @03:38PM (#8846964)
Calling that 'generics' is even weirder. What's so generic about a list of ints? That sounds pretty specific to me...
Re:Type hinting for Python by rayiner on Tuesday January 06, 2015 @03:26PM (#8846754)
Angle brackets suck.
Re:Type hinting for Python by frowaway001 on Tuesday January 06, 2015 @05:17PM (#8847481)
There 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.

Re:Type hinting for Python by frowaway001 on Tuesday January 06, 2015 @03:28PM (#8846783)
There 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.

Re:Type hinting for Python by frowaway001 on Tuesday January 06, 2015 @03:28PM (#8846775)
There 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.