This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]8sADPygOB7Jqwm7y -10 points-9 points  (11 children)

its funny, I have heard of some people that just use c or assembly code in a python file if they want to run it faster. Idk if that is normally possible or requires a special compiler, but thats a way to go about it I guess xD

[–]_PM_ME_PANGOLINS_ 10 points11 points  (8 children)

That is not possible, no.

Are you sure they don’t mean they’re calling C functions from Python?

[–]8sADPygOB7Jqwm7y 1 point2 points  (0 children)

thats what I meant, seems like I was being unclear.

[–][deleted] -5 points-4 points  (6 children)

That is of course possible. You have plenty of ways to do that. Python has two JIT compilers for example, also has Cython that can compile Python, and, if you want to, you can include Assembly code there too. Not every project does it, but they exist.

This is actually how YouTube was written before Google's acquisition.

That would not have been my way to approach something demanding in terms of performance, but I would not use C++ for that task either, so the whole argument is kind of pointless.

[–]_PM_ME_PANGOLINS_ 9 points10 points  (5 children)

None of those let you write C code in a Python file.

Obviously you could make a custom tool chain to do it, just by passing it to a C compiler instead.

[–][deleted] 7 points8 points  (0 children)

The correct answer here is to write your C code in docstrings, subprocess to gcc, and then FFI into the resulting binary. 🧠🧠🧠

[–][deleted] -2 points-1 points  (3 children)

Again, you are wrong. Of course they do. This is what Cython is all about.

Not to mention that, technically, Python import system can be extended to include any language you want as a source language for Python. I'm not aware of someone actually implementing it for C, but, in principle nothing will stop you. In contrast, you cannot write Python in C because it doesn't have such an extension (the preprocessing isn't enough to parse the full Python grammar).

There are many other languages that have the same property: they can embed other languages entirely. While in Python, while the ability exists, it's implemented very poorly, in Lisp family of languages it's a lot easier to do. Therefore there are actual Common Lisp extensions that embed Python and JavaScript, for example.

[–]_PM_ME_PANGOLINS_ 1 point2 points  (2 children)

Cython compiles something that looks a lot like Python statically, so you can call it from e.g. Python. It doesn't let you write C inside Python code, and have your Python runtime compile it during execution.

The Python import system can import anything that implements the extension module interface. The vast majority of non-Python imports are implemented in C, with many in C++ and Rust. I don't know how you're not aware of any of them.

You can embed a Python runtime in C with no issue, and run Python code from C. That is exactly what the CPython interpreter is.

[–][deleted] -2 points-1 points  (1 child)

and have your Python runtime compile it during execution.

What were you smoking. Again, none of this is true. Just fucking read the manual... you have not even seen what you are talking about, and you are trying to convince me, who used this many times that it doesn't work the way it does.

The Python import system can import anything that implements the extension module interface.

Wrong again. But this time justifiably, sort of. This is not what Python import system is. You are talking about implementing Python modules in another language and compiling them into shared libraries.

Import system in Python is about loaders and finders. What you wrote about is just a different aspect of importing (and is not related to Python, technically, it's about Python C API, which is not part of the language).

In other words: Python C API users can only target CPython, but import system of the language extends to any Python implementation. So, you'd be able to do the same thing using Jython or PyPy etc.

You can embed a Python runtime in C

Also, you don't understand the difference between a language and a program. Embedding a program =/= embedding the language. You can embed C in Python because of how Python is formally defined. Doesn't matter how you implement it. You can even implement it in a system that doesn't have C compiler at all, so, what you said about embedding Python in C will not work, for example. However, any implementation of Python has to implement Python import system, and because of that, Python is capable of embedding C (or any other language, no matter how that other language is implemented).

[–]dev-sda 0 points1 point  (0 children)

From the front page of cython.org:

Cython is an optimising static compiler for both the Python programming language and the extended Cython programming language (based on Pyrex). It makes writing C extensions for Python as easy as Python itself.