Python: The Dirtiest Language Ever
This week I wrote Python for the first time ever, and boy is it dirty. Every time I write some code in it I feel unclean.
But Python. Oh my. What a bad language you’ve been. The way default values magically morph into global variables, the way that you can’t seem to control or deduce the scope of variable declarations and assignments. The documentation for interfaces has been sketchy at best for much of the time, possibly because there is not really any such thing as an interface in the way I understand it. It’s been a culture shock, to say the least.
My kids have been learning Python in school, and even they had a bad time with it. The school was teaching Python because it’s easy, but to me that seems wrong. The first language I was taught – as opposed to self-taught – was Pascal, a language designed to be typed, structured, compiled and precise. Learning in a language that has a lot of disciplines is a good thing, because it teaches good habits that stay with you.
Nevertheless, I have been persevering. You may be wondering why.
Despite my earlier rant, not everything is bad in Python. The real sweet spot for Python seems to be (for me) in command line scripting. When you feel that bash has run out of steam you can flip over to Python and work with something that’s richer. The aspects of Python that appeal in this scenario are (1) the ability to maintain complex state in variables and (2) the libraries and modules you can build on.
Let’s take variables for a start. If you’re scripting in bash you can always declare variables, or call on environment variables, but they are all pretty one-dimensional. In Python I can actually declare structured types via a JSON-like syntax and then tag on properties and arrays as needed. This is a massive improvement on bash.
The second is the modules. I have been migrating a command line tool with quite a few options and parameters. In bash the parameter parsing and help text took up about 300 lines of code in the end. In Python, using the click module, I have stripped nearly all of this out by using the built-in decorators. And made the tool work better as a result. And improved the help text. And improved the way output is printed to the console. And improved the error handling. And improved the testability. And…and…and…
Want to output your code as JSON? json.dumps()
Want to load a JSON response from a service? json.loads()
Make a PUT request to a REST API? requests.put()
At the start of the exercise I was unsure where I’d get to, or if I’d even get anywhere. In the end I covered so much ground in a short space of time I couldn’t believe it. I find that when I’m working as a programmer the feeling of productivity, of delivering great value in short time, is one of the most fuzzily rewarding feelings I get. I got it this week in spades.
Python you may be dirty, but I kinda like you.