From C# to Python

Making the transition to C# to Python wasn't nearly as drastic as I had thought. Maybe that is because it was my 8th or 9th language (maybe more, who keeps count), or maybe it was because Python is such an intuitive and clean language. Maybe both.

When I say transition, that's a bit strong. While I do do most of my development work in Python these days and it is my language of choice, I do still code bit in C# from time to time. Mostly, these days, it's work on a SQL Server SSIS Component called Lysine that I have built with my partner at Amino Software (it provides direct import of mainframe data, or EBCDIC, into SQL Server).

But back to my point of some the welcomed features of Python from an experienced developer:

List Comprehensions

This is by far my most favorite feature! I can't tell you how many times I have written simple loop structures throughout the history of my development career. What developer doesn't write lots of loops.

But I love being able to go from this in C#:

ArrayList al = new ArrayList();
foreach (String s in originalList) {
    al.Add(s.ToUpper());
}

and doing this in Python:

al = [s.upper() for s in originalList]

What's even cooler is adding simple conditionals to the mix:

ArrayList al = new ArrayList();
foreach (String s in originalList) {
    if (s.Contains("secret")) {
        al.Add(s.ToUpper());
    }
}

and it is still a single, expressive, and readable line in Python:

al = [s.upper() for s in originalList if "secret" in s]

How cool is that?

Dynamic, but Strongly Typed

The other feature of the Python language, that I initially found troubling but now quite enjoy, is the lack of having to specify type information for a compiler. As you might have noticed in the previous example, we have to tell the compiler that that we are going to use an ArrayList and a String before we ever run the code.

In Python, we just assign values to a variable and it gets typed at runtime. I initially mistook this for a variant style language, but this is not true. Types are very much alive and well in Python:

>>> a = 1
>>> type(a)
<type 'int'>
>>> b = '1'
>>> type(b)
<type 'string'>
>>> a == b
False
>>> a == int(b)
True

Beyond the features of being able to do cool things at run time like add methods to a class, it has a very welcome benefit in that the code remains remarkable clean an unencumbered by extra typing, making for more readable and easier to maintain code.

Functions Free from Classes

The fact that in C# when I want to create some utility type function, I am required to wrap it in a class, it a bit annoying. I would typically write a class and create static methods on it, but what does this yield as a benefit to a project? Yet again, I have typed more useless characters when compared with Python.