[Python-Dev] Use of Cython

Yury Selivanov yselivanov.ml at gmail.com
Tue Sep 4 15:51:37 EDT 2018


On Tue, Sep 4, 2018 at 2:58 PM Stefan Behnel <stefan_ml at behnel.de> wrote:
[..]
> Cython has four ways to provide type declarations: cdef statements in
> Cython code, external .pxd files for Python or Cython files, special
> decorators and declaration functions, and PEP-484/526 type annotations.

Great to hear that PEP 484 type annotations are supported.  Here's a
link to the docs:
https://cython.readthedocs.io/en/latest/src/tutorial/pure.html#static-typing

[..]
> > I know that Cython has a mode to use decorators in
> > pure Python code to annotate types, but they are less intuitive than
> > using typing annotations in 3.6+.
>
> You can use PEP-484/526 type annotations to declare Cython types in Python
> code that you intend to compile. It's entirely up to you, and it's an
> entirely subjective measure which "is better". Many people prefer Cython's
> non-Python syntax because it allows them to apply their existing C
> knowledge. For them, PEP-484 annotations may easily be non-intuitive in
> comparison.

Yeah, but if we decide to use Cython in CPython we probably need to
come up with something like PEP 7 to recommend one particular style
and have an overall guideline.  Using PEP 484 annotations means that
we have pure Python code that PyPy and other interpreters can still
run.

[..]
> > I'd be +0.5 on using Cython (optionally?) to compile some pure Python
> > code to make it 30-50% faster.  asyncio, for instance, would certainly
> > benefit from that.
>
> Since most of this (stdlib) Python code doesn't need to stay syntax
> compatible with Python < 3.6 (actually 3.8) anymore, you can probably get
> much higher speedups than that by statically typing some variables and
> functions here and there. I recently tried that with difflib, makes a big
> difference.

I'd be willing to try this in asyncio if we start using Cython.

Yury


More information about the Python-Dev mailing list