How learning Python made me a better C++ programmer

This post on the Raganwald blog spurred me to write about something I've been thinking about for a while: how learning Python has made me a better C++ programmer.

The short answer is, it convinced me to start using the boost libraries (and the Loki library). I'd seen these libraries before, and thought that they looked pretty neat, but that most of them looked too esoteric to be really practical.

But that was really a failure of imagination. Learning Python taught me the value of programming at a higher level. Things like using boost::signals to break up dependencies; boost::bind and boost::function to use functions as first-class objects; boost::foreach to separate iteration from the algorithm; boost::any for generic data types; and much more.

Kind of ironically, Python also taught me the value of using a functional programming style in C++. I say ironically because Python isn't a functional language (although it supports a functional programming style). It was reading the book Text Processing in Python (David Mertz) that showed me the value of this style; then as I got more into TDD, the value of this style became clearer.

Today, I couldn't imagine not programming at a higher level in C++. My C++ code is now shorter, easier to understand, and has fewer bugs and platform dependencies. But I might have never moved to this new level if Python hadn't shown me the way.

16 comments to How learning Python made me a better C++ programmer

  • she

    The question is, why Python?

    Because if a language x, which offers the same benefits which you experienced therein, comes along and is usable, why wouldn’t someone profit from this too, in a language like C++/C/C# etc..

    Btw this is more a rhetorical question. I guess my point is, for example if Perl vs Python would have made a big difference, or not. (I havent found an article like that about Perl + C++ for example)

  • Edd

    I was going to write a post with the exact same title! Maybe I still will as my reasons are somewhat different to yours.

    When learning Python, I really enjoyed level of polymorphism afforded by the whole duck-typing thing and wished that something similar was available in C++.

    Before long I realised that it was. With C++ templates and concepts you can get the same effect. It happens at compile time, rather than runtime, but the usage in the code is largely analogous.

    This really made me look more in to how to design with templates and I’m so much better off for it.

  • “Btw this is more a rhetorical question. I guess my point is, for example if Perl vs Python would have made a big difference, or not.”

    For me, no. I learned Perl pretty much at the same time as C++, but I don’t think it made me a better programmer in general. It did make me better at regular expressions, though :)

    I also learned Prolog and Lisp before learning C++, but I didn’t carry over much of what I learned into C++.

    Something about Python just clicked. Maybe it made it easier for me to think about programming in general at a higher level, or maybe I was just more experienced and was ready to grow as a programmer.

  • I too was going to write a similar article in response to the raganwald challenge. Might still do in fact.

    For me learning Python made me approach C differently (I don’t use C++). The lack of braces in Python made me start writing in smaller code blocks and the Python language made me focus on readability. I’ve taken this emphasis on readability back to my C programming.

  • Ryan Ginstrom

    @Edd
    Yes, duck typing is another good one. I also like boost:any to get run-time dynamic typing.

    @Edd & Jamie
    Looking forward to seeing your articles!

  • Re Boost.Any, you’ll probably find that Boost.Variant is better for most uses as it is type safe in a way that Boost.Any isn’t.

    If you have any Boost code to share, please head over to the Boost Cookbook and drop it in there. Especially any of it that shows Python idioms translated into C++.

  • Ryan Ginstrom

    “Re Boost.Any, you’ll probably find that Boost.Variant is better for most uses as it is type safe in a way that Boost.Any isn’t.”

    Thanks for the tip. I had kind of gravitated toward Boost.Any simply for the icky associations of the “variant” word :-), but I’ll look into using Boost.Variant more in the future.

    Great Website, by the way!

  • Paul

    Someone else discovers that C++ doesn’t do some things as well as higher level languages! If you need/want these capabilities, you’re better off either using a different language or spending your time making it co-operate with C++. No amount of unreadable, undebuggable, not-very-portable bolted on kludgery is ever going to make C++ do lambdas as well as a language that was designed for that.

    I stick by my rules: no boost in production code, ever; STL beyond basic containers must be justified and approved; nesting templates (for example) is more likely to get you in code review trouble than virtually any use of macros.

    I call our policy “Aim low and SHIP.”

  • Ryan Ginstrom

    @Paul
    Have you been burned by boost or the STL before?

    For new development, my first choice is almost always Python. However, I have a lot of legacy C++ code to maintain, and I’ll continue to use C++ in speed-critical parts of my programs. In those cases, the STL and the boost libraries have made this a lot less painful.

  • Wolfgang Frisch

    “No amount of unreadable, undebuggable, not-very-portable bolted on kludgery is ever going to make C++ do lambdas as well as a language that was designed for that.”

    Interestingly though, the next version of C++, currently called C++0x will feature lambda constructs: http://en.wikipedia.org/wiki/C++0x#Lambda_functions_and_expressions

    Oher aspects well known to Python programmers will be in it too, like r”raw strings” and u”unicode strings”.

  • Learn OCaml and you’ll stop trying to be a better C++ programmer! ;-)

  • 89 Ways for You to Become the Coolest Programmer in the World…

    Since there are dozens of posts on becoming a better developer, but no single post with all the advice you need, perhaps, you’ll find this short guide useful.
    1. Learn the Skills You Need

    Learn the programming basics

    “The goal of this guide is to b…

  • Arthur Klassen

    Jamie Bullock’s link doesn’t forward correctly to his article. Try this link instead.

    cheers…ank

  • When I was looking for a job, they asked for C++ and they consider it was an advantage to know Phyton. Now that i started to learn it … I am glad i did. you are right about everything you said in the article.

  • János

    I 100% agree with the post, as I love python for its clean typing; usable built-in objects like list, dictionary, set; C++ like operator overloading; dynamic evaluation; testing possibility in the interpreter window and so.
    I have started learning C++ 5 months ago, with a strong C and Python background and I have to say that, python helps me a lot to use C++ on an efficient way. With STL I can use the same objects (vector, map, … and logic) those made python short to code. Python classes make possible to code python in C++ style, so I can test the algorithm in the python interpreter. I can see that C++ is much faster, than my python, but prototyping in python is faster compared to do it in C++. The winner toolset for me is testing in python in C++ style and turn it to C++ after that. Python also can teach any C++ programmer to think on a more abstract level and jump off from the C base. I have seen a lot of bad C++ source samples those only mimic OOP, but miss its advantages. The well tested python libs can suggest clean directions to design usable classes. Python with C++ seems to be a very kindly company. I just recommend to learn both of them.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>