Look at any discussion over some shortcoming of Python syntax, and somebody is bound to mention the following guideline:
There should be one– and preferably only one –obvious way to do it.
Basically, it goes like this:
Person: why doesn’t Python have support for <feature x>? This is pretty basic.
Python advocate: <feature x> is already (poorly) supported by <feature y>, and we can’t have two ways of doing things.
Why aren’t there switches? If-elses do the job! (or dicts)
Why aren’t there block comments? Line comments (or triple-quoted strings) do the job!
Why aren’t there increment/decrement operators? Augmented assignments (or regular assignments) do the job!
Why aren’t there anonymous functions? Lambdas do it, or just defining and naming the function elsewhere does the job!
This is very common. It’s also mostly stupid. Here are some examples where this guideline has not been followed:
- Those examples I just gave above. Note how, when missing a feature, there are multiple workarounds. I would think that clearly adding the functionality would make there one obvious way to do it, rather than the current system of two not-so-great ways.
- Block comments: either put # before each line, or wrap it with
- Augmented assignments:
x += yis another way of doing
x = x+y
- For loops.
for x in y: blah
is another way of doing
z=iter(y) while True: try: x=next(z) except StopIteration: break blah
- Tuples, lists and dicts: dicts could do everything (as evidenced by most languages just having one way of making an “array”)
This isn’t exhaustive. It is all I can think of at the moment.
All I’m trying to show is that it’s clearly not the case that there is only one obvious way to do something. And if you’re thinking “but in each of cases, one is clearly better than the other in the right circumstances!” you are correct, but note what I said above: if some missing functionality were supported, that would become “the” obvious way of doing it. Who’s going to use an if-else chain if there is already support for switches?
Remember there are other parts of The Zen of Python, including “Although practicality beats purity.” (item #9)
No comments found.