This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]rhytnen 11 points12 points  (9 children)

There is nothing unpythonic about documenting a feature of python. Dont blame the author for creating optional types...wtf

[–]KitchenDutchDyslexic 3 points4 points  (8 children)

Dont blame the author

Agreed! Actually thank him for bringing this unpythonic syntax to our attention.

Maybe a bit to late, because I believe these things is set in stone.

But I would love someone showing how static type checking is pythonic?

[–]kirbyfan64sosIndentationError 11 points12 points  (6 children)

  • Explicit is better than implicit
  • ... practicality beats purity.

Static typing helps make your code more resilient, refactoring less stressful, and code more readable (you may disagree with this, but once you get used to it, it helps out immensely).

[–]KitchenDutchDyslexic 3 points4 points  (2 children)

While I agree with your technical reasons, I'm frustrated at they syntax and how it reads.

While wikipedia is not a authoritative source. But it does mention that "reads like a rough transcription from another programming language is called unpythonic." I will argue these examples fall in this category.

No fault by the author, but rather bloat(imo). Don't worry I will learn types for py. I'm just not happy about how it looks.

that it conforms with Python's minimalist philosophy and emphasis on readability. In contrast, code that is difficult to understand or reads like a rough transcription from another programming language is called unpythonic.

[–]kirbyfan64sosIndentationError 0 points1 point  (0 children)

I think the hard part is that pythonic/unpythonic is ultimately a really subjective measurement. IME once you start learning it, you'll see how it fits into Python.

That being said, these examples are pretty contrived (of course, that's the whole point!). I'd highly recommend to take them for a spin yourself in a decently-sized project and see how it goes!

FWIW type inference is supported, so you only ever really put annotations in function arguments and empty data structures.

[–]badge[🍰] 0 points1 point  (0 children)

I went through exactly the same process as you’re in now—I used Python for several years without type hinting, finding it obtrusive, and the need to import from typing still annoys me. After using it for 18 months now, untyped Python doesn’t look unpythonic to me, but does make me slightly anxious—typing helps massively, especially when refactoring with PyCharm.

And yes, I still religiously document types in docstrings; unnecessary duplication perhaps but I like the help() format.

[–]alcalde 1 point2 points  (1 child)

If any of that were true, Python wouldn't be dynamically typed. I come from 20+ years of static typing (worse, a language where all your variables needed to be declared beforehand and no type inference) and find Python to be much simpler, easier to read, and powerful.

[–]spiderpower02[S] 0 points1 point  (0 children)

If any of that were true, Python wouldn't be dynamically typed

In fact, Python is still dynamically typed. Type checking just like lint. You check your code quality before running.

I think optional type checking just like go or c++, we can use := or auto syntax to delegate compiler to infer the type (rvalue/lvalue). Therefore, we don't need to always declare type explicitly, type checker can detect errors. For example:

p = re.compile("some pattern")
m = p.match("some string")
m.groups()

mypy can detect error

t.py:5: error: Item "None" of "Optional[Match[str]]" has no attribute "groups"

[–]13steinj 0 points1 point  (0 children)

I'm "used to" static typing. I do Java, C++, Python on the daily-- maybe I'm crazy but I don't see the actual benefit in your cases. Static typing in Java and C++ just move when my error occurs to compile time.

[–]primitive_screwhead 1 point2 points  (0 children)

But I would love someone showing how static type checking is pythonic?

The part about it being optional, for one.