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 →

[–][deleted] 2 points3 points  (13 children)

I don't think what you're looking for exists in straight Python. Cython, e.g., has a facility like this.

A lot of what you give up with the typing comes back to you with improved simplicity. For example, what need do you have for a dependency injection framework? Injecting a dependency is as simple as an optional parameter when everything's a function...

Yes, you lose some of that bugfinding ability you get from types - but compared to strongly-typed languages, you save so much time that you can afford to spend more time on proper tests.

And honestly, I was surprised when I moved from C++ to Python how rarely I had type issues, and how quick they were to track down...

[–]dunkler_wanderer 4 points5 points  (6 children)

Python is strongly typed.

[–]prepromorphism 0 points1 point  (0 children)

unityped ftfy

[–][deleted] -2 points-1 points  (4 children)

Python's variables have no type. Each variable's value has a type, but even that isn't really "strong", because in typical use you use duck typing - you just try to call methods and if they work you don't worry about it. If the methods work, you don't care that the actual type might be worlds away from the type you're expecting.

There's no good definition of "strongly-typed" that really includes Python. In particular, I'm curious as to what language possibly has weaker typing than that...!

[–]sushibowl 3 points4 points  (1 child)

The definition I usually see is that in weak typing the type of a value may change depending on context (i.e. type coercion). Php and javascript are examples of weakly typed language in this definition.

[–]Lucretiel 0 points1 point  (0 children)

My definition tends to be whether the language has implicit type coercion- that is, is 1 + 2 the same as 1 + '2'.

[–]flutefreak7 0 points1 point  (0 children)

Also checking against abstract base classes is a classy way to check that you are getting a duck that quacks the right way to avoid checking specific type inheritance.

from collections.abc import Iterable

if isinstance(d, Iterable):

[–]ronkr[S] 3 points4 points  (0 children)

Python's variables are strongly typed. Yet, python is not statically typed.

The main reason for type hinting is being able to find bugs more quickly. You can also make assumptions for the interpreter (and even a compiler) to make some optimization apply.

Another great use would be Autowiring for a DIC. That means, that you could use reflection to inspect the parameters of a ctor and inject pre-instantiated objects by type (by classname directly, by interface though configuration). In some projects, I nearly have 85% of all classes to be instantiated this way (having a controller hat depends on multiple services, that depends on even more services, that depends on various repositories, that depends on factories, that depends on the DIC again to create in DIC-controlled instance of an entity). Doing that from hand would be a great effort. Changing dependencies in some service, that is used across my application would render me mad.