News for hackers. Stuff that matters.
Re:Hidden features of Python by ajross on Monday September 22, 2008 @01:12PM (#311357)
It's been about a year or two since I've done anything serious in python, and ... yikes. Does anyone else get the feeling that Python is "C++ing" itself into a confusing mess? While lots of these elements weren't surprising to me, some were. And many that were convey little to no real value (c.f. "decorators", or "the step argument in slice operators").

This is the same thing that happened to C++. If you keep adding features that are useful to someone in some specific context, what you get is a more powerful language for an expert user writing code, but one that is much less productive for the people who have to maintain it.

And, can I just ask: how many ways to iterate over stuff does python really need?

Re:Hidden features of Python by lacker on Monday September 22, 2008 @01:40PM (#311395)
Well, a lot of these suggestions are the sort of "tricks" that you wouldn't want to use in any normal situation. I think the additions to python are mostly making it cleaner and simpler.

List comprehensions are a great example. If you want to reverse the keys and values of a dictionary you can do

  reversed_dict = dict((value, key) for (key, value) in original_dict.items())
Whereas in c++ this would have to be something like...

  map<string, string> reversed_dict;
  for (map<string, string>::iterator it = original_dict.begin();
       it != original_dict.end(); ++it) {
    reversed_dict[it->second] = it->first;
  }
C++ may have added a lot of new stuff but they did not make the simple cases simple, they just made the simple cases possible but still ugly.
Re:Hidden features of Python by ajross on Monday September 22, 2008 @02:07PM (#311425)
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.

Re:Hidden features of Python by shadytrees on Monday September 22, 2008 @02:56PM (#311484)
List 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.
Re:Hidden features of Python by anamax on Monday September 22, 2008 @03:12PM (#311513)
Python 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.

Re:Hidden features of Python by lacker on Monday September 22, 2008 @04:34PM (#311600)
I 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.

Re:Hidden features of Python by alexk on Monday September 22, 2008 @03:47PM (#311551)
1. 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.

Re:Hidden features of Python by aston on Monday September 22, 2008 @05:42PM (#311679)
List comprehensions aren't syntactic sugar. They're actually faster outright than for loops are. Especially if you're aiming to use the resulting list.
Re:Hidden features of Python by mrtron on Monday September 22, 2008 @01:35PM (#311389)
Decorators are greatly valuable.

For example, in a framework like Django they are used for permissions in views. So you can quickly just make sure a user is logged in or is an admin to see a specific view, all with an un-obtrusive @login_required.

I don't find any of these features bad - they are easy to read and useful.

Re:Hidden features of Python by shadytrees on Monday September 22, 2008 @02:46PM (#311474)
Agreed. The decorator idea didn't just come out of nowhere: It merely codified a convention that appeared in a lot of code where `function` would be defined and then you'd see `function = decorate(function)` afterward. (This is, for example, arguably the core idea of WSGI.)
Re:Hidden features of Python by ajross on Monday September 22, 2008 @03:06PM (#311502)
Right. So ... why bother with adding a feature to the language when the language as it already exists can do it in one easily-comprehended line of six tokens? I'm sure you like the shorter syntax better. But I didn't know it, despite years of experience with scripting languages (and even this paradigm) that should have informed me about what that code means.

But it didn't, for the simple reason that it looked funny because I haven't been keeping up with Python. I'll say it one last time: this idea, that the language should cater to its experts by adding features wherever possible, is what has killed C++ as a hacker's language. Python is walking blindly down the same path.

Re:Hidden features of Python by natrius on Monday September 22, 2008 @03:24PM (#311525)
"So ... why bother with adding a feature to the language when the language as it already exists can do it in one easily-comprehended line of six tokens?"

The decorator syntax is easier to read. It's at the top of the function where the arguments list and docstring are. Yes, there are tradeoffs when introducing new syntax, but decorators are definitely worth it.

Re:Hidden features of Python by schtog on Monday September 22, 2008 @05:53PM (#311694)
is it? python3000 has deleted some unnecessary stuff. i dont really see your concern. it seems gvr is still concerned about the opposite of what you claim.
Re:Hidden features of Python by cstejerean on Monday September 22, 2008 @01:53PM (#311406)
I wouldn't blame the language for code being hard to maintain. As long as the people doing the maintaing are as competent as the ones that wrote the code to begin with, and the code is well structured, tested and documented there should be no problem maintaining and extending it.
Re:Hidden features of Python by alexk on Monday September 22, 2008 @03:05PM (#311500)
So, what's your favorite language?
Re:Hidden features of Python by timmy2shoe9384 on Thursday September 25, 2008 @05:01AM (#314690)
Does it matter? There all worthless QQ ZIBHACKWAY the question is...... what is your favorite code?