7 ms·
Pretty impressive that the syntax: def greeting(name: str) -> str: Turns out to be backwards compatible in Python3 before 3.5 (though not Python2, yet anot
by ghshephard 11y ago
Pretty impressive that the syntax:
def greeting(name: str) -> str:
Turns out to be backwards compatible in Python3 before 3.5 (though not Python2, yet another reason, if you actually needed one, to start new stuff in Python3)
- ianamartin 11y agoI'm insanely happy that I have a new job that is not stuck on Python 2.x This is one reason. All my personal fun-type projects are in the latest Python, and I have the leeway to update work to this. I know there are a lot of people who don't like or want this, but coming from a C# background before Python, I really like this feature. (not that I want Python to be more like C#, I just like the safety net and guaranteed documentation.)
- heyalexej 11y agoThe "typing"¹ backport package on pypi claims to be compatible back to 2.7. EDIT: A gem i found on Reddit: Configure setuptools to install it only if required². [1] https://pypi.python.org/pypi/typing https://pypi.python.org/pypi/typing [2] http://stackoverflow.com/a/32643122 http://stackoverflow.com/a/32643122
- jsmeaton 11y agoHow is it backwards compatible? Was there syntax added to the language in preparation?
- ghshephard 11y agoI have no idea how they managed to find something that didn't throw a syntax error, yet looked reasonable. Perhaps someone who followed the process will chime in.
- deleted 11y ago[deleted]
- koenigdavidmj 11y agohttps://www.python.org/dev/peps/pep-3107/ https://www.python.org/dev/peps/pep-3107/ They are just arbitrary expressions (though in Python, type names like 'int' or 'str' are themselves objects). They don't do anything by default but are accessible as fields of the function object.
- Walkman 11y agoIt was in there for a long time, just nobody used it.
- kqr 11y agoCorrect. Annotations were added in 3.0 with no encouragement to use them for anything in particular, so they could observe what the community would use them for. They were just put in a dictionary attached to the function, and by inspection you could access them and do whatever the heck you wanted with them. A lot of third-party libraries cropped up that used them to type-check stuff. Type-checking was a popular enough usage for them it's official now, to help gather people under the same libraries and terminology. You can still use the annotations for other things, of course – the dictionary is still attached to the function and can be inspected as before – but if nothing else is specified, we can now assume they are type declarations.
- daveguy 11y agoThat's the syntax? Looks to me like another reason to stick with Python2.
- aroberge 11y agoIt's optional typing: you don't have to use it with Python 3.
- fulafel 11y agoThat sounds more like a C++ than Python design sensibility.
- coldtea 11y agoTo me it sounds like addressing a pain point for larger projects, moving the language forward, and adding something many in the community has long wanted (as evident by blog posts and articles, earlier type checking tool attempts, the acceptance of the relevant PEP, etc).
- fulafel 11y agoYes and that sounds unfamiliar from Python cultural POV. Python used to be the language that didn't try to please everyone and kept a coherent design philosophy that resulted in a clean and simple language.
- _yy 11y agoStill is clean and simple. I like the new syntax.
- creshal 11y ago> Yes and that sounds unfamiliar from Python cultural POV. Addressing pain points is unfamiliar to the Python culture? In practice, type intransparency is a serious problem – duck typing is only fun as long as everyone a) knows what a duck is and b) agrees on what it should do. Type hints make it a lot easier to work with opaque libraries and inside large codebases.
- rdtsc 11y agoIt was intended to be so ;-) This was talked about and was there for a while. https://www.python.org/dev/peps/pep-3107/ https://www.python.org/dev/peps/pep-3107/ [2006!] These weren't specifically called "type" annotations but I think that was in the back of everyone's mind.
- sitkack 11y agoFor Python 2 you could greetings.__annotations__ = {'name': str, 'return': str} Same exact effect. The syntax isn't the magic sauce, the type checker is. http://www.mypy-lang.org/ http://www.mypy-lang.org/ https://github.com/google/pytype https://github.com/google/pytype
- nikolay 11y agoWhat I don't understand is why it's not: def greeting(name: str): str: or: def greeting(name: str): str Using once ":" and then "->" for type declaration is inconsistent. Not to mention that the dash is not always aligned with the tip of the ">" and it looks ugly, not like a real arrow!
- ghshephard 11y agoThe name: implies that the type of the "name" object is str. If you were consistent, then, def greeting(...):str would imply that the type of the greeting object is str, which is definitely not the case (it is "function"). The -> isn't defining the type, it's specifying what is returned.
- nikolay 11y agoBut it's not the name alone, it's the name with the signature. `greeting` might be `function`, but `greeting("nikolay")` type would be `str`.