0

Does it possible to combine 2 different programming languages on one project?

let me give you an example of what i mean:

My main programming language is Python.(Python 3)

Lets say i want to write a calculator, i want build the GUI using C and than build the calculator itself in python. The GUI written in C need to send the data that the user input to calculate to the Python code.

Is it possible?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Personally I like using `ctypes` for this, but there are also Python extensions, SWIG, and other methods. – Lee Daniel Crocker Apr 10 '15 at 18:09
  • *Is it possible*: yes. See https://wiki.python.org/moin/IntegratingPythonWithOtherLanguages – Martijn Pieters Apr 10 '15 at 18:10
  • 1
    The usual pattern is to write a gui, which is not cpu bound, in Python, and write some of the calculations in C. CPython is written in C and the stdlib is a mixture of Python and C, with C used for time-consuming operations. – Terry Jan Reedy Apr 10 '15 at 19:35
  • Platform? Unix is very different here than OS X and Windows... – dawg Apr 10 '15 at 20:12

2 Answers2

1

For those two languages in particular, this guide should have nearly everything you need to know:

https://docs.python.org/3.4/extending/index.html

As a rule of thumb, any good interpreted and/or VM-based language can:

  • Have extension modules written in the same language as the interpreter.
  • Allow you to embed the interpreter in other programs, again written in the language the interpreter / runtime was written in.
  • Often, but not always, allow easily "wrapping" an existing native shared library.

This holds true for Java, Python, CLR languages, and Javascript, as well as some less commonly known languages that are typically used because they are easy to extend or embed (e.g. Lua and TCL).

Mixing & matching interpreted and/or compiled virtual machine based languages is often achievable by virtue of the fact that nearly every major language in this category has been re-implemented in every other one in addition to the often "standard" C impl of the languages.

For example, there's PyPy, Jython, and IronPython for Python.

And if all else fails, run two separate processes, one in each language, and use an IPC mechanism like sockets to talk between the two.

Brian McFarland
  • 9,052
  • 6
  • 38
  • 56
0

Yes. You'd have to convert the Python code to a DLL that the c code can call, which sounds difficult but possible.

Community
  • 1
  • 1
John Wu
  • 50,556
  • 8
  • 44
  • 80