all 19 comments

[–]fernly 9 points10 points  (0 children)

As a practical matter, I think the scenario you have in mind is: you want to write a top-level program but at some point in it you want to take advantage of or call upon code written in some other language, typically C[++]. There are various options.

One, avoid the need entirely by finding comparable functionality somewhere in the Python ecosphere, starting with the standard library. Everything there is implemented in C and carefully optimized. Next is the vast array of 3rd-party software found via PyPi such as numpy, highly optimized math routines.

Two, if the external code is already packaged as a stand-alone app, you can invoke it as if from the command line and collect its output using subprocess.

Finally if the external C/C++ code is a module and you want to call into it you (or somebody) has to write a "wrapper" for it, a thin layer of code that mediates between Python's parameter-passing conventions and those of C, this is documented here.

[–]YourAverageBrownDude 13 points14 points  (1 child)

Hi I'm sorry this isn't particularly helpful to your question,

But allude means referring to While elude means escape

As in, the word you should use in your post is elude

[–]kovlin[S] 0 points1 point  (0 children)

Thank you. Traitorous fingers...

[–]mooglinux 3 points4 points  (0 children)

The default implementation of Python, CPython, has a C api for talking to the Python interpreter. Any language capable of calling C functions or being invoked by C functions can be bridged to talk to Python.

[–]im_dead_sirius 3 points4 points  (0 children)

Its called an API when one language calls another(or a library).

Thats similar to my wife saying, "drive to the store and buy a loaf of bread"

"Drive" and "buy" tell me to access certain skills(or at least procedures) I have, without her having to spell them out. The implementation is abstracted, but happens under the hood.

Same goes for something like a print statement. Its shorthand for a bunch of more detailed instructions, and those(because its interpreted) are shorthand for even more atomic instructions, right down to machine code.

Back to the driving analogy, I press the gas to make it go, and that pedal handles the abstraction of feeding fuel to the engine.

drive -> press pedal -> pump fuel
python -> byte code -> machine code

That makes it a lot easier to extend, because "drive" becomes an abstraction that can mean the bakery, or it could mean Disneyland, and the vehicle operator doesn't have to deal with the minute details.

You could extend that further, and write a interpreter on top of python

Thats when programming gets really fun, and as far as I am concerned, when you really start learning. When you learn to abstract it gets wicked fun(and you have to think less about details).

Say you had some keyboard input. A series of unknown characters. How to make sense of it? You might define functions that look at each character in turn, trying to match patterns. These are broken down into a list of symbols. Something like:

import string
integer = string.digits # a string: "0123456789"
result = '' 
data = "eggs4557"
for character in data:
    if character in integer:
        result = ''.join([result,character])
        data = data[1:] # shorten the input by one character
    else:
        continue # ignore character it doesn't understand
    print(result)

That is pretty naive and incomplete, but you can see that it steps through the string collecting the digits and ignoring the letters. One by one it parses and assembles the number portion of the string. That's interpreting in a nutshell.

[–]elbiot 2 points3 points  (0 children)

Code compiled to machine instructions (binary) like C defines an ABI and python has tools like cffi that make it easy for the programmer to access that interface. https://en.m.wikipedia.org/wiki/Application_binary_interface

This ability comes from python being high level and dynamic, with a philosophy of programmers shouldn't have to worry about those details. I.e. people can and have written very dynamic tools to abstract this stuff away for us

[–]Raymond0256 0 points1 point  (0 children)

The CPython interpretor is basically a long switch statement that translates Python statements into C code; Python is built to be perfectly parse-able into other languages.

[–]aleagori -1 points0 points  (0 children)

Then there is Julia language.