0

I am writing an application where I am coding all of my front-end and GUI with python library (wxpython specifically). For this application, I would like to write the model class with C and use python to use the compiled C code? How can this be implemented in python?

I know this is little vague question but I am struggling with the starting point.

2
  • 1
    Cython might be an option for you. Commented Mar 19, 2012 at 19:03
  • 1
    Are you looking for ctypes? docs.python.org/library/ctypes.html Commented Mar 19, 2012 at 19:03

3 Answers 3

1

If you're using CPython (the most popular version of Python), you'll need to learn the CPython C API. Other python implementations may or may not support C calls. You can also use the ctypes library which is easier to learn, but also more rigid and may not support everything you need.

Sign up to request clarification or add additional context in comments.

Comments

1

You can try Cython, which is basically Python with embedded support for C types and calls (it goes through the CPython API).

Comments

1

You could strictly seperate design(python part) and code(c++ part) like this:

Write a complete c++ programm that works in the terminal/console and then make the python-application call these c++-terminal programm via os.Popen.

So if your programm is a calculator it does this:

(python gui) 5 + 5 -> my_c_programm.exe "5 + 5" -> (returns) 10 -> (python gui) display

that way you can use your programm with and without gui.

Its easier and faster than embedding Python in your c++ programm or extending Python with C++.

I basically do the same thing on my current project, but like this:

php: my webinterface python: for structure and logic and easy operation c++: for heavy calculations and where I need speed

so php -> python -> c++

and it works very well for me :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.