The Artima Developer Community
Sponsored Link

Python Buzz Forum
Mixing Python and C++

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Thomas Guest

Posts: 236
Nickname: tag
Registered: Nov, 2004

Thomas Guest likes dynamic languages.
Mixing Python and C++ Posted: Feb 28, 2007 10:19 AM
Reply to this message Reply

This post originated from an RSS feed registered with Python Buzz by Thomas Guest.
Original Post: Mixing Python and C++
Feed Title: Word Aligned: Category Python
Feed URL: http://feeds.feedburner.com/WordAlignedCategoryPython
Feed Description: Dynamic languages in general. Python in particular. The adventures of a space sensitive programmer.
Latest Python Buzz Posts
Latest Python Buzz Posts by Thomas Guest
Latest Posts From Word Aligned: Category Python

Advertisement

A while ago, I expressed a desire to work on a mixed language system.

I can’t get over how neat Python is. Sometime soon I’d like to work on a mixed language system: Python for the tricky bits, C++ for the speedy bits.

I haven’t changed my mind. As usual, it’s been an evolution rather than a revolution – step by step, I’ve got closer to realising this ambition, and finally, now, I think I can say I’ve achieved it.

Why Python and C++?

No particular reason. C++ is the systems programming language I know best. When you need to get close to the silicon, it does the job very well. And Python is the high level language I know best. When you want to write concise, flexible software, it does the job very well. Really, though, I’d be just as happy to write the tricky code in any decent high-level language, switching in low-level languages as dictated by performance and platform requirements.

Mixed language development

Perhaps the most obvious way to mix languages is in a single program: you start off by writing the program in Python. If it doesn’t go quickly enough, you profile it and write a C++ library to look after the slow bits. There are disadvantages to this, though:

  • you have to figure out how to interface between the two languages
  • the library and the program can become interdependent and harder to test in isolation
  • your IDE and build system may not like it

As a consequence, this turns out to be a technique I’ve used relatively little. Here are some of other techniques I’ve used more frequently.

  • Code Generation. Python is a great language for generating code – either in Python or C++, or in any other language.

  • Multiple Processes. Rather than sharing a single process, the C++ and the Python can run as separate executables. C++ does the heavy lifting; Python manages the complex application logic. A side-effect is that the operating system can use separate processors for the separate processes without all the problems associated with multi-threaded code.

  • Prototyping. Sometimes a compiled C++ program is required. I’ve found that sketching out a prototype in Python then simply rewriting it in C++ can work well. The prototype can be used as both a reference implementation and a source of test data.

  • Testing. Python is a great tool for creating test data and for running tests.

  • Refactoring. C++ lacks the refactoring tool support available for (e.g.) Java, but simple Python scripts can do the job well enough.

Read: Mixing Python and C++

Topic: Respecting the whole person Previous Topic   Next Topic Topic: Evaluating WYSIWYG editors

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use