Interesting differences between Python and C++

This is not a post about praising or bashing either language, but rather an objective account of the differences I discovered myself while working through the Invent With Python book. These seem interesting to me, so here goes:

[1] This is still a bit fuzzy to me, but in Python, variables are in scope even outside the loops in which they are first instantiated. True, we do not need to declare variables in Python, as opposed to C++, but intuitively I would still have thought that a variable won’t be visible or retain its value outside the loop it is first used in. However, as the screenshot shows, Python recognizes the variable r outside the for loop in the function and returns it. C++ however, throws a compile time error when it sees the code in the corresponding screenshot.

Python recognizes variables outside the loop

Python recognizes variables outside the loop

C++ does not recognize variables outside the loop

C++ does not recognize variables outside the loop

[2] In Python a function may accept an argument of any type, and return a value of any type, without any kind of declaration beforehand. For example, in the screenshot, you see the function might accept a boolean and return a boolean, or it might accept an integer and return a string or vice versa. Even though Python is strongly typed in a sense that you may not add a string and an integer without an explicit conversion. For example in Perl, something like this is valid:

Multiple argument- and return-types in Python

Multiple argument- and return-types in Python

my $a = “A”;
my $b = 3;
my $c = $a.$b;
print $c.”\n”;

And it produces the result A3. However, you may not do the following in Python:

>>> a = ‘a’
>>> b = 3
>>> c = a + b
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: cannot concatenate ‘str’ and ‘int’ objects

Yet this kind of flexibility that Python provides in calling functions and returning values makes our code incredibly simple and much more maintainable. Perl provides something like that too but Python looks cleaner, is object oriented, and still maintains a little strictness about types. Again, this post is not about bashing a language or promoting another.

[3] The division of two integers in Python is always a float (in Python 3; or in Python 2 if you import division from __future__), as opposed to C++ where you need to do some casting to make that happen.

[4] The round function in Python always rounds intelligently – round(4.3) is 4 and round(4.9) is 5, and it even works for negative numbers. In C++ there’s no round function; there’s floor and ceil, but you need to know beforehand which one to use.

Floor and ceil functions in C++

Floor and ceil functions in C++


7 thoughts on “Interesting differences between Python and C++

  1. Python is dynamically typed but it is strongly typed. So is ruby. Javascript and perl on the other hand are dynamically typed and weakly typed.

    The real power a ruby or python gives you is the ability to return functions from a function. That is the genesis of meta programming. Code that writes code!

    About scoping of variables: It depends on the lexical scope. In ruby for instance

    >> def e
    >> 1.upto(8){|i| r=i}
    >> puts r
    >> r
    >> end
    won’t work because {} is like an unnamed function which “closes-over” r. After reading this article it would make sense

  2. If you really wish to learn meta-programming, I’d suggest Lisp or scheme. They are the languages which implemented meta-programming in true sense. However, getting to unlearn infix notation and learn prefix is just too much at times.

    You may start with Ruby. Ruby has very good metaprogramming chops. You can do things like opening up a class (any class even base classes like Float) and adding methods to it. Generating methods and classes on the fly or on demand. You can trap “no method condition” and generate new methods etc.

    The main thing that metaprogramming provides is the ability to shoot yourself in the foot! There is hardly any boundaries. But that provides the flexibility to mould the language to meet your needs than the other way round.

  3. Sounds good. I know quite a bit of Haskell, which they say is the ‘next-generation’ Lisp or Scheme. I did go through some video lectures on Scheme but to be honest, I am not fond of the parentheses-deluge πŸ˜‰ You should look into Haskell if you haven’t. The syntax is one of the cleanest ever. Of course there are some differences. I/O is kind of hard in Haskell. It is also complicated, if at all possible, to interface the methods themselves to other languages. I mainly did it for functional programming, and less so for metaprogramming. My next language is going to be Scala, which is a hybrid of functional and other paradigms, and can be interfaced easily with Java. Either that or F#, if I decide to go the Microsoft-way.

    For now though I must say I am happy with Python. Finished the gaming book, going through Dive Into Python and will eventually go over Programming Collective Intelligence, which has real NLP applications [the type that Amazon and Netflix use] literally programmed and discussed, in Python. You never know if I’d start contributing to a real open source project in Python.

  4. To be honest,with three years working with python and ruby and to some extent php and JavaScript,i can say they all have dirty syntax and their emphasis on speed and reliability is often over stated.I will stick to c++ and c .

    • I have heard bad things about JavaScript – that a newline can totally change the semantics of the code =) And as much hatred as C++ gets, that’s only because it is used so widely. Speed is certainly not something Python and co can do better than C++ – Python has its beauty, but indeed in seasoned circles, nobody would consider you a programmer at all unless you can speak C++.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s