From C++ to Python

Share:Tweet about this on TwitterShare on RedditPin on PinterestShare on FacebookShare on LinkedInShare on Google+

 

This post is more theoretic than usual and I will analyze briefly my experience on passing from C++ to Python and what I found interesting to notice when learning another programming language is needed.

 

Introduction

After working with C++ for a while, I had to learn Python for the first time and use it with Panda3D engine. Right from the beginning I’ve found myself thinking in C++ style a lot of times while programming in Python, especially for the first couple of weeks. Finally I realized that the first programming language you master (or maybe your favorite one), can affect the way you program and think in other programming languages. Probably this thing is also true for the spoken languages.

When you learn your first programming language, you are learning also all the best practices and the techniques of the language itself. This type of notions aren’t merely syntactical and they belong to “programming theory” or “IT theory” rather than a specific language. But when you starting to master this language you create common mind paths for creating instructions and more complex modules that belong only to the specific language you’ve mastered. Obviously these are my opinions, I’m not an expert in psychology.

 

Programming mind path

To help passing from the C++ “mind path” to the Python ones to me has been pretty easy after all, but I recalled some struggling at the beginning. Now let’s analyze the two languages to try to facilitate the journey.

First of all, let’s talk about some actual differences from Python and C++:

  • Python is run through an interpreter and is interpreted each time it runs., whilst C++ is pre-compiled
  • In Python, there is no need to declare types explicitly
  • Python uses Garbage Collection whereas C++ does not.
  • C++ supports pointers and low-level memory management.
  • C++ is a statically typed language, while Python is a dynamically typed language.

 

As you can see in the previous short list Python and C++ are both object oriented languages but they are ruled by different philosophies. Beside these strict differences that are also some intrinsic principles and styles of coding that belong to the specific language and find place in the deepest part of a programmer’s mind, the mind paths. This more “abstract” side of the differences of usage is difficult to manage at the beginning and I think it takes some time to deal with it.

 

Things to remember

Fortunately the syntactic side of the differences is less deep-rooted and can be managed more easily: there are only some little things that can make you struggle, especially at beginning of the switch. I’ve listed a series of things I found useful to “easily” switch from C++ to Python.

  • Indentation and curly brackets: in Python there are no curly brackets to define a block of code but instead of this it use indentation
  • Loops with float: in Python you can’t easily define a loop with a float increment
  • Data structure declaration: Python has built-in lists and dictionaries and you can declare it simply with “[]” for lists and “{}” for dictionaries
  • There’s no separate char in Python
  • No switch: Python doesn’t have switch statement and you have to implement it with if, elif blocks
  • While-Else: the loops have the else statement which allows to do something when you go out of the switch
  • Use less lines: Python allows you do simple things in less lines than C++. For example: a = 5 if b == 4 else 6
  • Python allows to write if statement in a single line inside an assignment. It’s not mandatory to use this feature but since you have the chance, why shouldn’t you?
  • Function pointers: in Python there are no control on pointers in general, but since the assignment works on reference, you can do similar things. When you have to use something that works like a function pointer, all you have to do is to declare a variable and assign to a function name (without the brackets) and that’s it. Since everything is a class in Python (even functions) and everything is “callable” when you have to call the function pointer, all you have to do is to call the variable.

 

References:

  • A Byte of Python – Swaroop

 

Game developer & designer. Unity 3D lover. Movie fanatic.

Game developer & designer. Unity 3D lover. Movie fanatic.