Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The State of Optional Static Typing in Python?

I've been playing with Typscript for a while now, and I gotta say, bundled with the fact that nodejs is faster than the current implementation for CPython for my web development needs, I've been more inclined to make more things with it.

In fact, I've made a couple of basic apps with it, even for desktop. What I love about Typescript is the fact that is has optional static types. This makes coding often a lot easier, and not to mention the intellisense is amazing for when you want to code quickly and get a prototype running. I've encountered fewer bugs in the development process as well.

But here lies the problem, I've been working with Python for a year now, and I love the language, and I am just more used to the syntax. However, more and more, I see that optional static typing can be an awesome plus point for Python. So, I did a bit of research, I first started with trying to understand what the situation of static typing is right now. Just like anyone else, I used a search engine to find out.

These are some of the sources of information that I've read through:

  1. Adding Optional Static Typing to Python

If you take a look at the implementation here (excerpt from the link above):

from StringIO import StringIO

def foo(f: StringIO) -> str:
    f.write("hello")
    return f.getvalue()

f1 = StringIO("booh+")
print foo(f1)  # prints "booh+hello"

Now, this looks like very similar to the implementation in Typescript. However, if you take a look at the date, it goes back all the way to 2004.

Then, I took another look at a slide that the BDFL, Guido made:

  1. Why Add Static Typing?

Again, you see the same rhetoric. However, I've not been able to find any new information regarding this.

I've also taken a look @ this SO answer on enforcing static types.

So, I wanted to ask, what is the situation or state of adding optional static typing in Python? Or do we need to invent something like Typescript but for Python 3?

I believe that optional static typing is important, because in my own very limited experience, I've seen that something like Typescript and C# work better since when making complex classes or making large projects, I've seen that static typing has gone a long way to helping me write code faster and error free. I am sure I'm not nearly as good as most of the developers here, but that's my take. This is why I feel that this question is an important one. I mean if you had the optional typing of a language like C# with the beautiful syntax that Python provides, then Python would probably be unstoppable (yay!).

I've done some research into this, but I got nothing substantive, so I was hoping that some of the awesome and wise members of SO would shed light on this issue.

like image 271
Games Brainiac Avatar asked Aug 21 '13 17:08

Games Brainiac


People also ask

What is static typing in Python?

Static type checks are performed without running the program. In most statically typed languages, for instance C and Java, this is done as your program is compiled. With static typing, variables generally are not allowed to change types, although mechanisms for casting a variable to a different type may exist.

Is Python dynamic typing or static typing?

Python is both a strongly typed and a dynamically typed language. Strong typing means that variables do have a type and that the type matters when performing operations on a variable. Dynamic typing means that the type of the variable is determined only during runtime.

What is static typing in programming?

A statically-typed language is a language (such as Java, C, or C++) where variable types are known at compile time. In most of these languages, types must be expressly indicated by the programmer; in other cases (such as OCaml), type inference allows the programmer to not indicate their variable types.

What is typing module in Python?

Introduced since Python 3.5, Python's typing module attempts to provide a way of hinting types to help static type checkers and linters accurately predict errors.


1 Answers

Or do we need to invent something like Typescript but for Python 3?

That's mypy !!! :-)

Personally, I think either they are going to merge or Python is going to come up with their own solution. Hope this happens very soon!!!

like image 109
Mario Rossi Avatar answered Oct 05 '22 20:10

Mario Rossi