The last few weeks i’ve been dabbling around with Python. I’m busy doing some experiments with OpenCV, and because it has Python bindings it seemed like a good excuse to dive a little bit more into Python.
- Python is mostly readable after a few months of not looking at it. I know the language is designed to do this, but it’s still pretty amazing when you dig up a script from a few months back and you don’t feel like reading Finnegan’s Wake.
- Things usually do what they should do. There’s little confusion over methods. Getting the contents of a file is simply
f = open("file.json").read()
- The module system is easy enough to not overload with boilerplate when writing simple scripts, but powerful enough for writing large applications. It’s even easier than how Node.js does it, no module.exports, simply define functions, import the module and Bob’s your uncle.
- It’s a small thing, but it pleases me that writing
defis shorter than
- Package support is a lot better, it’s like the Mac app rule: packages simply seem better, especially if you compare it to the awfulness that is NPM modules (esp. in regards to documentation). Most packages seem written with the fact in mind that other people than the original coder are going to use it.
- Python seems to strike a nice balance again between readability and longevity in the method names. It’s simply
sys.exit. It’s a lot better than the confusing short PHP method names (
nl2br) and the boring long Java methods (
- Even though i’ve read otherwise, for most of my uses, Python is fast enough.
- Googling for common questions virtually always leads to a relevant, well written StackOverflow post.
- Debugging seems easy. Most errors are readable and you never get the awful ‘white page of death’ like in PHP.
- The internal
helpmethod is awesome, especially when combined with the powerful REPL. I love the fact that
import mymoduleeven works in the REPL and you can do little tests and experiments.
Could be better
- The documentation is not very good, especially compared to PHP. Reading the Python docs feels more like reading a language spec than something that helps you solve problems. Especially, there’s a lack of simple and clear examples. If i want to know something i usually Google something and end up at StackOverflow. That shouldn’t be the case. Even the Node.js docs seem more readable.
Here’s an example. Let’s say i want to know how to do the Python equivalent of PHP’s
foreach. The docs about the for statement say this:
The for statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object: for_stmt ::= "for" target_list "in" expression_list ":" suite ["else" ":" suite]
Right, so what does that mean? What’s ::=? What’s an ‘expression list’?
The expression list is evaluated once; it should yield an iterable object. An iterator is created for the result of the expression_list. The suite is then executed once for each item provided by the iterator, in the order of ascending indices.
“Ascending indices”? “Suite”? This might be clear for a CI professor, but it’s not for the mere mortals that read the docs and want to get stuff done.
And after reading this section i still don’t know how to do foreach in Python :(
Nice that the search works on the offline download though.
- ‘Variable hiding’ is confusing. I realise you shouldn’t be using
ifas a variable name, but because of ‘variable shadowing’ stuff like
idmight also not your best choice for a variable name, even though it makes a lot of sense. The convention seems to be to put an underscore after the variable (
id_) but that looks like a clunky hack.
- Adding to that, having modules in a directory that have the same name as a module give really weird error messages. For example, try naming a module
stat.pyand then run
lambdasyntax seems somewhat…funny. Why can’t we have multiple-line lambda’s (yes, i know that’s why). For now, defining a function in a function and returning that feels like a kludge. Especially when doing anything complex with map or filter this is irritating.
- Why can’t we have dot access to members of a dict? Writing dict[“instruments”][“guitar”] instead of dict.instruments.guitar because boring pretty fast. Furthermore, the difference between dicts, tuples, objects and what works as an options object in a function is confusing.
- pip seems to do its job, but i’m still a little confused by how it works. What if i need a different version of a module than the one installed globally? Composer and NPM seem to handle this better.
- It’s not as easy as PHP to write a simple webpage, although Flask makes it as easy as writing an Express.js app.
- I should probably write something about Python 3 here. But on the other side, i haven’t really had any trouble with it. I guess. I don’t even know which version of Python i’m running. That might be something positive as well, i can’t count the number of times i’ve been cursing because a server didn’t have PHP 5.3 installed and i couldn’t use anonymous closures.
- Why can’t we have json.loads(“file.json”)? The
json.dumpsis awesome though.
So, that’s my list. What are the things you like and hate about Python?
If you like this post, why not give a vote on Hacker News?