Monday 7 January 2013

Python - Warning Lessons


Over the last fortnight I've been dabbling with Python, the seemingly ubiquitous language seeping into every nook and cranny not containing a dedicated C++ follower, like myself.

Its on the Pi, its behind a lot of research work and big companies, like Google (before they worked on Go - lots of their stuff was Python, and perhaps still is).

So, I took a look... Its a powerful language, I don't doubt, it reminds me very much of writing procedural Pascal form the mid 1990's, but most importantly it looks like psuedo code, other authors mention this (http://www.jayconrod.com/posts/37/a-simple-interpreter-from-scratch-in-python-part-1) and others even crow about the syntax and structure being so simple.  But I hate it.

It leaves so many stones unturned, so many open ends, and I have to say it makes me cringe.

In C++ one has to definiately define the article of the body of a class, for example:

class Alpha
{
private:
int m_Value;
};

Now, the value of Alpha can only be set by functions which are part of this class, it is totally private:

class Alpha
{
private:
int m_Value;
public:
Alpha (const int& p_Value)
: m_Value (p_Value)
{
}
};

We can all agree this is a powerful tool, it lets us change and range and control the use of variables in the class, it can make our code safe and remove bugs, also the type of m_Value is implicitly defined one can't define m_Value and then start to point values at m_value by mistake, the compiler tells you to go away, this in my opinion is correct programming, I agree there's a lot of overhead (if you let the compiler create all that code) but there are far less potential bugs.

In Python we can get the same class:

class Alpha:
    def __init__(self,p_Value):
        self.m_Value = p_Value

And I find this abhorrently dangerous, first off, I didn't have to create "m_Value" the interpreter simply saw the name and invented it, if I then use "m_value" or any typo the interpreter does not thing, Ah hang on I don't know what this value is, it thinks "Ah a new thing to work with" and creates said typo as a new value.  Debugging such things is an utter nightmare.

Then the syntax itself, its clean, though not neat the reliance on tabbing to define structure annoys me.  I already have a big bee in my bonnet about different indentation styles in the C language family, so to see indentation made a part of the language, white space defining function, is scarey as hell.

But, Python is out there, and its a language I don't know an aweful lot about, so I'm learning it, gradually on the side.  Just you guys and gals out there learning it be very wary of its pit falls!

No comments:

Post a Comment