3 ms·
I like (most of) this list! Note, however, that most of these are more for writing Python code, whereas my article was specifically about designing Python APIs
by benhoyt 3y ago
I like (most of) this list! Note, however, that most of these are more for writing Python code, whereas my article was specifically about designing Python APIs -- that is, the public-facing stuff.
A few specific comments on your points:
> If a loop can be expressed by a comprehension then it should be.
Not sure about this one. I prefer comprehensions if you're filtering or transforming without side effects (other than creating a new list). But I prefer a regular for loop over things like [os.remove(p) for p in paths if p.endswith('.pyc')], otherwise the side effect (the important bit) kind of gets lost, and it's harder to handle errors.
> Inheritance and multiple inheritance in Python is fantastic. Prefer it over composition.
Oooh, that one's up for debate. I won't enter into the fray here though. :-)
> Remove the word lambda from your vocabulary.
Mostly agreed, apart from tiny sort key functions and the like.
> One file one class is for Java people, in Python pack it all in one file.
Agreed, thanks for saying it!
> from package import Thing is preferred unless the module in question is just a collection of functions.
This is okay if you're importing from a module made to be used this way. But I disagree that it's better! See my rationale under https://benhoyt.com/writings/python-api-design/#module-and-package-structure https://benhoyt.com/writings/python-api-design/#module-and-p...
- Spivak 3y ago> Not sure about this one. I prefer comprehensions if you're filtering or transforming without side effects. That's what I was trying to drive at with if it can be replaced but you said it a million times clearer.