r/learnpython • • 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

0 Upvotes

13 comments sorted by

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.

-10

u/Dzhama_Omarov Jan 03 '25

Well, GPT at its current state already surpassing some people I know (unless you ask how many Rs in the strawberry😄), so it feels right to me to use he or she

Thank you for your reply! You’ve actually answered the question I didn’t know I had, whether it’s better to learn Cython or it’s better to learn an other language

-2

u/codypoker54321 Jan 03 '25

I'm surprised chatgpt gets such a basic question wrong, strawberry appears to have 3 R's actually.

I have no idea why this comment is downvoted, you seem like a very polite and fun young man.

-1

u/Dzhama_Omarov Jan 03 '25

Thank you for your kind words! Well, sometimes you can’t understand what stands behind actions of other people and we just have to live with that :)

As for GPTs answer, it’s very interesting to find what causes this „bug“. I call it a bug because when you ask GPT to spell the word he says „r“ three times and he is aware that there is one letter in the first part of the word and two at the second part

-1

u/codypoker54321 Jan 03 '25

yes, chatgpt spelled it out for me also. we all have choices, which is why I'm working on being a better person daily.

I really appreciate you speaking with me, do you have any other Python questions I can help with? I have 4 years experience, I can help

1

u/Dzhama_Omarov Jan 03 '25

Thank you for your offer! I don’t have any questions yet, but if I get any I know whom to ask :)

-1

u/codypoker54321 Jan 03 '25

thanks for being so nice. I know today is gonna be a great day now

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

u/[deleted] 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

u/[deleted] Jan 03 '25

[deleted]

6

u/throwaway6560192 Jan 03 '25

OP is asking about Cython, not CPython.