How I Learned to Stop Worrying and Love Python
I’ve used a few different programming languages over the past few years, ranging all the way from just a tiny bit of assembly, up to the ridiculously abstracted Java EE. Recently I’ve started using Python a lot – both at work and for side projects.
Given that, for a number of years, I’ve scoffed at Python, I thought I’d talk about why I’ve come to accept Python – and what issues I still have with it.
Some things really standout about Python as being consistently good about Python – and are largely why I’ve been using it.
By easy, I don’t necessarily mean easy to learn – in fact, someone who’s learned a few C-style syntax languages may find it somewhat confusing to use Python. That being said, when it comes to writing anything complex in Python its often very easy.
Take multi-threading – not only is it easier to create the thread itself in Python than many languages, but it also includes a variety of utilities for communicating amongst threads, like queues and events. Decide that you need to spawn off a full process to better take advantage of your multi-core system? The library for multiprocessing is almost identical to that for threads, allowing you to swap threads for processes with minimal code changes.
##There’s a Lot of Third Party Libraries
It seems like everything you could want to do already has a third party library in Python that you can use.
RESTful webserver? - Check! (Flask)
Machine Learning? - Check! (scikit-learn, mlpy, PyML, PyBrain, and more)
Astronomical Data Analysis? - Check! (stsci_python - Space Telescope Science Institute)
##It Manages Packages Well
Python provides its own package management scheme to let you easily obtain third party libraries. This means that you don’t have to manually download installers for Windows, nor do you have to hope that your distro’s repositories have the latest version of a library in Linux.
Beyond that however, it also provides a virtual environment tool, which will install python binaries, and let you install libraries into a folder of your choice. This means that you don’t have to clutter up your development system as you explore the different packages that will let you do what you need to do – and also lets you avoid using root privileges to install packages.
Of course, it couldn’t be all good, now could it? I’m sure all of these have some sort of solution to get around, but I’m looking at the base language for these issues.
It goes without saying – Python can’t be as fast as C. All the layers of abstraction make it a lot easier to program, but it’ll cost you in speed.
As a result, you’ll often see libraries that need speed – such as machine learning libraries – are wrapping C code to allow them to go faster.
##Approach to Object Oriented Code
Unlike languages like C++ or Java, Python doesn’t have things like the abstract keyword. While all of these things are core elements of being “Pythonic”, I simply don’t enjoy this approach to object oriented code.
Want to make a method abstract? You’ll have to raise a NotImplemented exception, and override the method in the child classes. And if you forget to implement it? You won’t know until runtime. Similarly, Python lacks support for interfaces – making collaboration with other teams more difficult.
Additionally, there are no public or private variables. This seems to be a jab at developers who think making a variable private provides security (it doesn’t) – but unfortunately it does take away one more thing you can do to prevent an accident (you can use ‘__’ prepended to the variable name to make it harder to access – but this isn’t as clean as private).
##Everything is a Runtime Error
One great thing about compiled languages? If you made a syntax error, you’ll know about it before you go to production. In Python, you won’t find it until you execute the code with the mistake. The upside to this downside, is it forces conscientious developers to test their code thoroughly, which is always a good thing.
The parts where Python scrunches up its face, and sticks out its tongue at you.
##Python 2 versus Python 3
While a lot of libraries exist for Python, many of them still only work in Python 2. While there are workarounds for this, for a language where everything is easy like Python, you’d think it’d just work.
Python uses indentions to for functions and other code blocks. Unfortunately, this means if you switch between editors, and one uses tabs and one uses spaces, you get an error – say if you wrote most of the code in PyCharm, and made a quick edit in Vim.
Also, I miss my curly braces.
Because real programmers program assembly in emacs. With their teeth.
No seriously, sometimes you miss finding an excuse to use weird constructions involving function and void pointers.