2 ms·
You're comparing the python syntax with the equivalent C++, not with the python syntax it replaced, which by my count is three lines shorter. The point is this
by ajross 18y ago
You're comparing the python syntax with the equivalent C++, not with the python syntax it replaced, which by my count is three lines shorter. The point is this: does the savings in code size from a list comprehension (which is 100% pure sugar on top of the existing loop syntax -- there's no new semantics in that feature) balance out the difficulty of teaching them to every new python programmer?
If you're a python expert, clearly your answer is "yes! this is a great feature!". But that's what the C++ experts say too, even as their language buries itself under a mountain of complexity. From the outside looking in, I'm seeing exactly the same thing happen to python.
Some fancy features make a language fun and productive. Too many such features make it a mess.
- shadytrees 18y agoList comprehensions don't exist in their own vacuum though. For generators (the generalization of list comprehensions) and then coroutines, it's a feature that can't be replicated in Python without serious kludges; it does indeed add semantics. It enables you to do more: threads, actor model, state machine, modularize large functions. I think the cost of implementing list comprehensions was more than worth the while for all the benefits that have been reaped from it.
- anamax 18y agoPython could have had generators without having lisp comprehensions, so you don't get to use "generators good" to conclude "list comprehensions good". List comprehensions were initially syntax for loops over generators. The current version introduces another variable scope.
- alexk 18y ago1. Can you list a couple of features of C++ you are talking about? 2. Why do you thing that list comprehensions are so difficuilt to learn? iterators, generators and list comprehensions are easy and clean concepts. If you want a feature, you write a lib. Once the feature is widely deployed it becomes a recognized pattern, so why not to include this one into the language? Look at lambda or bind in boost - they are scary, but people still use them, since it's a powerful concept, and it would be much better to include them into the language.
- lacker 18y agoI think you're making a good point, which is that the new python features are usually more "syntactic sugar" than they are "features". It is not very hard to teach people that alist = [foo(x) for x in things if isok(x)] is equivalent to alist = [] for x in things: if isok(x): alist.append(foo(x)) Similarly it's not hard to explain what an @login_required or a @memoize decorator does, although it is tricky to write a decorator yourself. Same goes for generators. I don't think syntactic sugar is a bad thing. I wish C++ had better syntax. If I could never type hash_map<blah, blah>::iterator ever again, I would be happy. But that isn't the sort of feature that C++ tends to add.
- aston 18y agoList comprehensions aren't syntactic sugar. They're actually faster outright than for loops are. Especially if you're aiming to use the resulting list.