r/learnpython • u/Dzhama_Omarov • Jan 03 '25
Are extensions like Cython really turn Python into a compiled language?
I was chatting with chatGPT the other day and he told me about compiled and interpreted languages. He’s also told me about extensions like Cython that aim to make your Python code compiled. So, I was wondering if they really give your code the benefits of compiled languages or they work in a different way
P.s. I apologize in advance if my question makes no sense. I’ve never dealt with compiled languages and I have vague knowledge about them
4
u/billsil Jan 03 '25
Python code is compiled to pyc and then interpreted.
Cython is a separate language that's similar to python, but more like C, but really different than both and lets you compile Python to C. You can then import a Python module into a larger Python program or larger C program. Honestly, unless you get good at it and/or C, you'll probably find it slows down your code for a lot of work. I prefer numpy's f2py to convert Fortran code.
2
Jan 03 '25
Cython is a superset of python which does compile to c, if you're looking to run some c code or wanting to speed up your python code significantly then definitely look into cython.
It allows you to write in a python and c hybrid language with nearly all the features of both languages. Only downside is it also has the drawbacks of making code much harder to debug and write.
Cython requires cpython to run the cython modules but if you combine with a python compiler such as nuitka you can remove this dependency
2
u/JamzTyson Jan 03 '25
Cython does not turn Python into a compiled language. Python is, and I expect will always be an interpreted language.
I have never seen a non-trivial program written entirely in Cython. The much more common approach is to implement performance critical parts of a Python program in Cython.
The introcuctory paragraph on https://cython.org/ provides a good description of what Cython is.
0
17
u/eleqtriq Jan 03 '25
I like that you called ChatGPT “he” 😂
Cython is an extension, but it’s really a superset of Python. It’s still requires the CPython runtime. It’s best used to make modules IMHO, that can be imported into Python and used. To make true use of it, you also need to use type annotations, otherwise the benefits are “meh”.
By using Cython, you’re not going to get an executable.
In my opinion, if you’re looking at Cython, you might as well use another language. I feel the scope of good use cases are narrow, such as graphics processing where an already existing library doesn’t exist.