Reputation: 7631
Recently Python 3.6 added static typing as a way to enforce certain types. This same functionality I used to get it from Cython, obtaining highly optimized functions when compared to vanilla Python.
My question then is: Will we also get substantial a performance increase when using the new Python static typing? pros/cons of each approach?
Upvotes: 7
Views: 3436
Reputation: 8059
There is no static typing in any existing version of CPython, 3.7 or earlier.
The support for optional type annotations in CPython 3.6 (backported to 3.5 as well) helps the external tools such as static code analysers to verify that types are used consistently in a program.
Type hints have no impact on bytecode compilation or execution.
From CPython 3.6 What's new:
In contrast to variable declarations in statically typed languages, the goal of annotation syntax is to provide an easy way to specify structured type metadata for third party tools and libraries via the abstract syntax tree and the annotations attribute.
Note that however type hinting syntax can be used in Cython to define C types (Type Declaration Syntax).
Upvotes: 7
Reputation: 94
Static typing in Python doesn't make it a compiled programing language. Therefore, performance-wise, you should always get better performance from Cython (Compiled should always beat Interpreted).
The main purpose of Python's newly added static typing is to perform type checking in a seamless way, bys sacrificing some of Python's philosophy on the way.
In short: Cython for speed, Python3.6 for interpreted/more pythonic approach.
Upvotes: 7