Just because I’m that kind of person, let me go back to yesterday’s article on setting environment variables to talk about Python and why I think it’s much better suited to many tasks.
So let’s look at the same example in Python:
registryKey = \
'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
variableName = 'SOMEVAR'
value = 'Another String bites the dust!'
registryHandle = OpenKey( HKEY_LOCAL_MACHINE, registryKey ) SetValue( registryHandle, variableName, REG_SZ, value ) CloseKey( registryHandle )
SendMessageTimeout( HWND_BROADCAST, \ WM_SETTINGCHANGE, \ 0, 'Environment', \ SMTO_ABORTIFHUNG, 5000 ) So what are the advantages? At first glance, you’ll see the Python program is short – half as short as the C++ sample (13 vs. 27 lines ). “How come?”, you ask. One main reason for this: Exceptions
Error cases in Python are consistently handled as exceptions. There’s no need for lines and lines of error checking that do little else besides propagate the error to the next layer – which is what most C++ error handling does.
“But… aren’t exceptions only for truly exceptional cases?”
Short answer: Yes. Long answer – if a function doesn’t do what I expect it to do, that is an exceptional case. And at least with exceptions, I’ll know the moment it happens. Most C++ code silently ignores return codes, which hardly qualifies as a better solution.
Plus, I happen to think that error codes really are out-of-band communications, which is much better expressed by exceptions than by return values or extraneous parameters.
“But you could do that in C++, too!”
Yes, I could. If I wanted to write even more code. Exceptions are not part of the language philosophy, which means many of the APIs don’t support them. Python internalized exceptions, and the API reflects it. My goal is to write short, expressive code. If there are standardized building blocks to do that, I’m happy to use them – but C++ really doesn’t have much in terms of a support library. Which happens to be a point in favor of any post-Java language – they come with massive APIs that solve most of the basic problems for you, so you can concentrate on the problem at hand.
There are a couple more pluses to Python, so let me mention them really quick:
- Dynamic Typing
I don’t have to spell out that a string is really a string, and a number is really a number. Variables in Python take the type of whatever they contain. Types in C++ are for the most part busy work – you’re just spelling out something again that you’ve already said a few times. - Indentation
Yes, that’s right. I love the mandated indentation format for flow control – because it forces even the sloppiest of programmers to indent their code so I can read it. C++ programmers do the same thing – they just call it a coding standard and fight to the death over it. Me, I’d rather focus on the problem instead of layout opinions.
There’s plenty more, but that’s for another day.
“If Python is so great, why didn’t you use it right away?”
Fair question. I actually did use it to prototype the solution, but two things conspired to make me convert this. First, I wanted to use SendNotifyMessage() – and there’s no Python binding for it yet. (Yes, that is a drawback of Python – I didn’t say it’s perfect!). Second, the article was mostly a summary of older notes for a colleague at work – so I picked a language he’s more comfortable with.
“So what other reasons are there NOT to use Python”
Oh, plenty. The main one I’m hearing is plain old fear of the new. I hope that’s not what’s stopping you from trying it out. However, there are some real limitations. If you need to go “direct to the metal” – i.e. interface with the OS or some hardware – you need bindings. Which usually require a dose of C/C++.
Even more importantly, if you need raw performance, stay away. If your application is slinging around hundreds of megabytes per second, Python might not be your tool of choice. You do pay a price for all those exceptions and dynamic types. The usual advice in that case is to “write the performance critical part in C/C++, the rest in Python”. That might work – I’ll defer you to others who’ve actually tried it.
Or maybe you already did try and can tell me if it worked?
Great, thanks for the code.