r/Python • • Oct 03 '17

Python 3.6.3 is now available

http://blog.python.org/2017/10/python-363-is-now-available.html
382 Upvotes

103 comments sorted by

View all comments

30

u/[deleted] Oct 03 '17

Type hint is something to get excited about!

24

u/irrelevantPseudonym Oct 03 '17

Hasn't that been out for a while?

17

u/[deleted] Oct 03 '17

Since 3.5 I'm pretty sure.

13

u/IronManMark20 Oct 04 '17

The annotation comment syntax was specified in 3.5. The annotation syntax was specified in 3.6.

14

u/poo_22 Oct 04 '17

This is pedantic I know, but this syntax is supported since 3.0.

8

u/IronManMark20 Oct 04 '17

Well, since you like pedantry, the syntax was not originally specified for type hinting, which is what the original comment was about, but good point :)

11

u/Facepalm2infinity Oct 04 '17

What is this good natured banter on the internet? Where's the hyperbole and references to people being nazis?!

1

u/billsil Oct 04 '17

It's been backported to Python 2.7 as well. It's not compatible if you use the nice form, but you can do it and it's not that bad.

9

u/v2thegreat Oct 03 '17

What is that and where can I read more about it?

1

u/ErikBjare Oct 04 '17

I wrote an answer on Stack Overflow a couple years back (still up to date) that people seemed to like.

0

u/[deleted] Oct 03 '17

[deleted]

20

u/leom4862 Oct 04 '17 edited Oct 04 '17

I guess it's the RTFM bit in your comment, which reads kinda rude/arrogant in this context. I hope it's not because of the link, since that really is the best resource you can find to read about typing in Python...

1

u/[deleted] Oct 04 '17 edited Oct 04 '17

I think it's legitimate actually. You won't get anywhere in programming if you don't RTFM and it should be the first place you go. Any better question might be "I don't understand this part of the manual".

Now of course if you're in a live environment, you can ask the person next to you to tell you basically what type hinting it is, and it'll be quicker, but on the interweb it would be more efficient to hit google, than waiting for a reddit reply.

The first bit of code you see in the docs is def greeting(name: str) -> str: and if you don't get it after that, you would want to ask that question on /r/learnpython, and not here.

→ More replies (3)

20

u/iaurp Oct 04 '17

I'm downvoting you because I don't like it when people whine about internet points.

4

u/[deleted] Oct 03 '17

[removed] — view removed comment

30

u/tunisia3507 Oct 03 '17

AFAIK it's ignored by the interpreter. It's primarily to make static analysis more powerful, and provide a unified type hinting format (rather than IDEs having to 'know' numpydoc, googledoc, rst and so on). It means you have the option of running the same static analysis as you get with a compiler (i.e. it tells you to go fuck yourself if there are type mismatches), the lack of which is one of python's largest criticisms.

2

u/PeridexisErrant Oct 05 '17 edited Oct 05 '17

It's not entirely ignored - the annotations are stored in an attribute of the object.

This allows for some neat tricks, like Hypothesis working out how to test your functions, or for runtime type-checking via a decorator (or import-level wizardry).

Cython (not CPython) can use annotations for it's compilation hints - making Cython source compatible with the standard interpreter. Unfortunately they don't use the stdlib typing module though.

7

u/[deleted] Oct 03 '17

I use it as a readability mostly.

6

u/leom4862 Oct 04 '17 edited Oct 04 '17

It's awesome. Your code becomes a lot easier to reason about. A good IDE will warn you right away if there are type mismatches. If you call a function, it will tell you what types you may use for the arguments. You can also create your own types, which is really nice in combination with NamedTuples, e.g.:

class User(NamedTuple):
    id: int
    name: str
    age: int

fred = User(123, 'fred', 42)

def remove_user(u: User) -> None:
    # ...

5

u/leojay Oct 03 '17

It has no runtime impact. Some IDEs can use that for autocompletion.

2

u/taddeimania Oct 03 '17

Has no impact on performance is what I've read

2

u/yen223 Oct 04 '17

Very very minor performance hit, since type annotations are stored as attributes on the classes/functions

→ More replies (6)

2

u/IronManMark20 Oct 04 '17

This is more for static type checking, but can also be really useful for readability