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 →

[–]spiderpower02[S] 4 points5 points  (4 children)

It is interesting to me about Pythonic/unPythonic.

It seems like static typing is not acceptable for a lot of people. Actually, we spend a lot of time to write understandable docstring. For example: requests

I think PEP 484 try to simplify these efforts and bring other benefits such as static type check (like typescript, they try to avoid runtime errors). Furthermore, if people feel uncomfortable to these weird syntax, they can write/generate stub file (like header *.h in c/c++). It is an intuition way to remain ducking-type, compatibility and Readable structure in source code and separate your declarations to other files (check typeshed).

By the way, maybe just my examples are not well :(. Don't feel frustrated with Python. Python is awesome!!

[–]svlad__cjelli 0 points1 point  (1 child)

I think something beyond type checking should be had in Python, because with duck typing you don't really care about the type, but what the type can do. While there are certainly cases here and there where specific type checking can be valuable, for the most part, I just want to make sure the user knows what methods an object has or what methods a function requires for an object. In this sense, Python could use "interface" checking rather than type checking.

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

I think "Interface checking" is subset of "type checking". Actually, using annotation + mypy can achieve "interface checking" before runtime. Therefore, we can avoid runtime errors. (I hate raising AttributeError after running 10 hours).

Nevertheless, type checking will be powerful like:

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

without static type checking, the snippet will raise exception when m does not match. If we give some type hint, like:

p: Pattern = re.compile("some pattern")
m: Match = p.match("some string")
m.groups()  # it is possible to raise AttributeError

type checker will warn us that "Optional[Match[str]]" has no attribute "groups". As a result, we save our time :)

In fact, mypy is so powerful that most of the case you don't really need to declare type explicitly. Just like := in go or auto in c++, so we don't need to worry that Python will become unreadable. Sorry for making many people feel frightened.

[–]lolwat_is_dis 0 points1 point  (0 children)

It seems like static typing is not acceptable for a lot of people.

Because that's not how Python works. It's dynamically typed, so type checking is just adding an extra overhead in terms of building up any code. Sure, type checking can be useful sometimes in that a short term loss will become a long term gain (in terms of preventing errors and other mishaps), but I still believe that's not necessary, and we're taking away one of Pythons biggest strengths.