Async Techniques and Examples in Python Transcripts
Chapter: Parallelism in C with Cython
Lecture: Why Cython

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Okay, okay I hear you.
0:01 You either don't want to write C, you would rather avoid
0:03 writing C, or you don't even know C.
0:06 Great, you can still write really awesome
0:08 code with your Python knowledge
0:10 using something called Cython.
0:13 Now, Python officially is known as its internal runtime
0:18 what's called the interpreter sometimes is known as CPython.
0:21 That's because the interpreter, the runtime
0:24 is implemented in C but you take
0:26 regular Python code and it is interpreted on top of it.
0:29 Cython is unrelated to CPython
0:31 even though they differ by only one letter.
0:33 Cython is an optimizing static compiler
0:36 for the Python programming language.
0:38 And what does it compile to? Well, it first compiles to C
0:41 and then C compiles to actual machine instructions
0:44 for your operating system.
0:46 It's as if you had written C code
0:49 but the syntax you had to write
0:51 and the libraries you get to use are Python.
0:53 It's beautiful and it makes writing C extensions
0:56 for Python as easy as Python itself
0:59 and you can bet it actually has some
1:01 threading implications along the way.
1:03 So, why would we do this?
1:04 Well, writing C, we already talked about some
1:06 of the advantages there but calling them out explicitly
1:09 we have, you can write Python code that at any point
1:14 can call back and forth between native C or C++ code.
1:18 So if you want to do any integration with other C code
1:21 or bring that into your system, guess what?
1:24 You can do that in Python back and forth.
1:26 Not with some weird integration layer
1:28 but just as if you were using that library directly.
1:31 You could easily take Python code and convert
1:33 it to high performance C code by just changing it
1:37 a little bit by adding static type declarations.
1:41 You get integrated debugging, so you can debug from
1:43 Python to Cython to C and back. That's pretty awesome.
1:47 You can interact effectively with large data sets.
1:50 Cython actually comes from the computational
1:52 numerical analysis side of Python
1:54 and the mathematical spaces and so on.
1:56 So it has good support with things like
1:58 Numpy which also, as I said, has big parts of it in C.
2:02 So why do you need to filter all of that data through Python
2:06 to get it over to the Numpy C stuff?
2:08 Just work directly here and you can
2:09 get some really good performance.
2:11 Because Cython can interact with the libraries
2:14 from CPython, you can pip install some random library
2:18 and use that.
2:19 All of the magic and power of CPython's ecosystem
2:24 is available to your what is effectively C code in Cython.
2:28 So that's pretty awesome.
2:29 You want to use requests from Cython, no problem.
2:32 Finally, you can integrate natively with existing code.
2:35 Is there some C library that you would like to use
2:39 but you want to write in Python
2:40 and how do you link those together?
2:42 Or do you have some big application that is written in C?
2:45 Well this is a really easy way to bring that into Python.
2:48 So these reasons and more are why
2:50 you might be interested in Cython.
2:52 There's a lot to it but simple use cases pretty simple
2:56 and we're going to get some really cool
2:57 performance outcomes from it.