Python for Decision Makers and Business Leaders Transcripts
Chapter: Python vs.
Lecture: Python with C++
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
So far, it's been versus. This or that. Python or MATLAB. Which one do you want? But when it comes to C and C++ Python was actually built with C.
0:12
If you look at the thing you download it's actually called CPython because it is written in C. It happens to run Python
0:20
but the implementation that makes it go is C. So Python integrates really well with C and C++. There's a couple of ways you can do it.
0:28
You can make libraries in C++ and here's the extension story on how you do that. You can import Python.h into your C++ file and off you go.
0:38
There's a library called CFFI or the C types module that lets you plug these two things together so you can write a core low level bit in C or C++
0:46
maybe it's taking to hardware or whatever and then it's very easy to turn that into a Python function or class that you can just work with
0:54
as you've seen so far. So this is a cool story but it goes further than that. There's also this thing called Cython CPython, drop the P, Cython
1:04
and it looks like pure Python. Here's a silly little math function where we're going along and just adding up the distance
1:10
or computing the distance over and over and kind of throwing that away. It doesn't really do anything useful but that's not the point.
1:16
Notice how we put type information on the variables. Do math start which is an integer and number which is an integer. That's normal Python.
1:24
We haven't done it yet. It's a newer feature. We could have done it all along and it's fine
1:29
but if you do this, you can actually run Cython against this and what it will do is compile it to C
1:35
and then at C, then it will compile the machine instructions and it's as if you had written this directly in C or C++.
1:42
Moreover, you can actually in this world in this Cython world, you can start importing and working with C functions and classes and so on
1:51
so you can directly create this boundary where C and Python touch each other but you still write Python code.
1:57
So here's another way in which Python and C and C++ come together. Finally, if you want to look a little more into this
2:04
check out this article over here on realPython.com Building a Python C Extension Module. It takes you step by step of what you actually have to do
2:12
to make C work with Python. It's actually not that complicated. Well that does it for our versus round. Hopefully those comparisons were meaningful
2:22
and gave you a good sense of what some of the trade offs are between all these different programming languages and technologies.