I can’t get Python off my mind. It’s an addictive language, and I’ve been using it so much at work that it’s just my go-to language. I love the syntax, I love the standard library, I just love it. Probably one of the most English-like programming languages, Python has become my main language as of late. I’ve been tasked with writing various scripts, and Python just comes through every time.
I no longer worry about how to read all of the command-line arguments my script might need, or how the configuration file will be parsed, or even the best way to read in XML. The modules are already in the standard library, I just import and go.
It really also helps that the documentation is really clear and to-the-point without excessive examples to clutter the readability of the page. I usually read other people’s examples to get to grips with how to do something — Fragments and ViewPagers never clicked with me until updating the Android Developer Tools, Eclipse and downloading the official examples — but have recently only needed the documentation, and that’s becoming a rare occurrence now that I’m far more familiar with my favourite modules.
What is so special about Python?
It’s not the fastest language and is a slightly heavy install, but it abstracts away so much (of course, it’s not the only language to do so e.g. Ruby, JavaScript, Lua, and countless others) that you probably don’t care about when writing a fire-and-forget script.
For example; I have a function that takes in a sequence of objects, checks equality of each object in the sequence with a control object, then spits out a result that states whether any of the objects in the sequence were equal to the control. Having to worry about types becomes a problem, then the flexibility of the types to be handled also becomes problematic. To make things easier, let’s look at some sample code (mind the rusty Java example).
Java
// Omitting class declaration for simplicity
public <T> boolean isInCollection(T obj, Collection<T> col) {
for (T item : col) {
if (item != null && obj.equals(item))
return true;
}
return false;
}
Python
# Purely a function in a module, no class declaration
def inseq(obj, seq):
return obj in seq
This uses just the standard in to check whether an obj is in a sequence. Simple as. It’s a slight cheat considering what I stated, but it works just fine, and is just so simple to boot. It doesn’t really even need to be in a function.
To make things a little fairer, here’s an example that checks whether the object is None first.
# Omitting class declaration again
def inseq(obj, seq):
return any(obj == item for item in seq if item is not None)
Simply put, that example loops through the sequence, assigning the next item to item, checks it’s not None with if item is not None, and then checks equality with obj == item. The built-in function any returns True if any item in the sequence evaluates to True, otherwise returns False.
The sheer simplicity with which that example can be achieved is amazing. The Java version has almost as much flexibility — though not allowing arrays (you’d need to overload the function for that) — through the use of generics, but requires the explicit null checks (though most equals functions check for null values, so this is a cheeky cheat on my end) and calling of the equals function on the object for comparison.
The power of simplicity is what really stands out in ‘scripting’ languages. That isn’t to say that compiled languages are not useful. Their power and performance benefits are what keep us coming back for more, along with making them the only truly viable solution for large systems. I have only gotten my hands dirty with C++ a small number of times since starting at BNP Paribas, but am preparing to do more of it soon, with C# being proposed for new development, and Java having been introduced in other departments. The speed that everything needs to happen at on the internal systems is so great that interpreted languages are never considered except as a joke. My own experiences with Python in the bank are just for fire-and-forget scripts, where I don’t care about the object’s type, I just find out whether it quacks or moos when I poke it into action. If the script takes five minutes to process a 100MB XML file, or a 250MB log file, the user waiting won’t really care once he or she realises that it didn’t require a lengthy login process to then use a heavyweight app just to get a few lines of results.
Who cares then? Big woop, wanna fight about it?
The power of simplicity is a wonderful thing, and would make an interpreted/scripting language a great candidate for teaching. Many of the newer generation interpreted languages have good object-orientation, but without bogging down a first-timer in objects when the first lesson is to print ‘Hello, world!’
This of course doesn’t mean you should learn to code, but for teaching kids, you don’t want to introduce them to memory handling, type-casting and object-orientation so early on. It will just scare them off with thoughts of complexity.
The simple nature of the language in question is great even for pros. You can prototype the logic of an application in a few minutes to a few hours, test your formulas, and make sure it runs exactly as you need it to. It can be used to fill the gaps, providing advanced users the chance to make their own functionality using extensions of the main application. Hell, use it as just another scripting language if you must. These higher-level languages have advantages in abstracting not only syntax, but the tedious low-level intricacies of the computer’s equipment, so that moving a file across discs and transferring data between machines is a far simpler operation.
I guess what I’m getting at is
Don’t hate, appreciate. I’ve talked to some programmers that think these languages are just for fun, that they’re just esoteric in nature, but haven’t given them a second thought and noticed them hidden power. Everything in the world of computers has its place and its uses, so stating that something is ‘completely and utterly useless’ does not generally bode well (even those fart apps have their place and use, after all).
Anything else?
Of course, as per usual, I have forgotten my original reason for even writing this post, and instead descended into opinion and drivel. I really just wanted to write about Python, add something to my completely ignored blog, and just get some writing practice in too.
On another note, approaching four months at BNP Paribas as a developer, and I have to say I like it. Things are rather relaxed and laid-back in my department, allowing me to work and learn at the same time without difficulty, and without the pain of whips at my back. I arrive early and leave late every day of my own volition, under nobody else’s will (I do try to stay later, but there are only so many hours I can sit at a computer for).
Since I’ve lost my train of thought, and it’s getting late for my to be typing away, this post is left, as always, feeling unfinished.