Programmer’s Log Programmers Writing Stuff – Garry Bodsworth

20Aug/085

How a C++ developer writes Python

I've been writing Python professionally for about a month and I am still in the process of shaking out my C++isms. This is not a diatribe about how C++ is rubbish, because I think C++ is great. I am making some "mistakes" as I am in the learning process.

So today I was writing a function to parse a URL into the useful parts that make it up. I'd laid it out all nicely, written some unit-tests and it did its job exactly how I wanted. Then I was asked did the Python urlparse module not do what I wanted. Damn - as a C++ developer I'm used to rolling a lot of stuff that make up the building blocks of the software, so what I did was pretty natural. What I need to learn is to check the Python libraries first before doing obvious things.

The other thing I am rapidly unlearning is writing verbose loops. In C++ I am used to writing loops int he way I am expecting them to be executed. In Python you need to express them differently using list comprehension, which I am liking a lot. I even rewrote some loops today I wrote in my first week where I replaced seven lines with one that expressed the intent of the statement perfectly.

Another thing that weirded me out was creating threads. You create the variables for them, start them off and they know when to clean themselves up... I think it is because I am still thinking about the C++ scoping rules rather than garbage collecting.

I am sure these Python-ing mistakes won't be my last, but it is interesting to wrap your head around some new things. I do miss manually managing my memory and knowing when stuff gets destroyed, I think it probably is because I'm paranoid and need to relax and go with the flow.

Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • StumbleUpon
  • LinkedIn
  • Tumblr
  • Twitter
Comments (5) Trackbacks (0)
  1. When I started in Python, I code Java with python syntax. Read a Haskell book and try to code the examples into python (a la functional) was a very good exercise. With python I can change quickly from/to OOP and functional programming. The problem define a paradigm, not the language.

    (Excuse my poor english)

  2. Hi there, thanks for the comment.

    Functional programming isn’t really a problem for me, I was just having issues context switching between C++ and Python styles initially. I think I have already made the step forward because I can see the loops I did in my first week needed to be replaced.

  3. When I first started coding in Python in 2000 I found myself writing a lot of standalone methods that weren’t class members. This bothered me as I’d written a lot of C++ and Java in 90s that followed orthodox inheritance & interface based OO design – the approach advocated in the GoF patterns book and writ large in Java’s class libraries.

    In the early 2000s my interest in C++ was reawakened by the rise of the template based generic programming approach, and I noticed the similarities between this style and the code structures possible in dynamically type checked languages like Python. I realised that class and inheritance based programming binds data and behaviour together too closely. It actually gets in the way of code and behaviour reuse. C++ templates, as in STL and Boost, and Python “duck typing” enable much looser coupling of behaviour to data, and therefore more reuse.

  4. C++, Isn’t that the hardest Programming language out there?!? Even VB is giving me a headache. 0.O

  5. It is really interesting to read this post. From C++ to Python, it is kind of from one extreme (we have to write lots of thing) to another extreme (dynamic language and intuitive language.).


Leave a comment


No trackbacks yet.