Adam DePrince wrote: > On Thu, 2006-03-30 at 23:37 -0800, Neal Norwitz wrote: >> On 3/30/06, Terry Reedy <[EMAIL PROTECTED]> wrote: >>> "Aahz" <[EMAIL PROTECTED]> wrote in message >>> news:[EMAIL PROTECTED] >>>> What do we want to tell people who have code like this: >>>> >>>> keys = d.keys() >>>> keys.sort() >>> Could a good-enough code analyzer detect such, even if separated by >>> intervening lines? If so, it could suggest sorted() as a fix. I wonder if >>> the pypy analyzer could be adapted for 2.x to 3.0 warning and upgrade >>> purposes. Or do pylint or pychecker gather enough information? >> pychecker is supposed to have this info, but only if d is known to be >> a dict. It could be extended to assume any method keys() (and >> friends) should return iterators. In which case, it would say that an >> iterator doesn't have a sort method. Below is the output of the >> current version. > > With the views that we were talking about before, with d.keys() all you > have is the view, neither an iter nor list have been instantiated. > > I'd almost say that > > keys = d.keys() > foo = keys.sort() > > could fit quite well into the view framework.
Not a good idea, since the long history of "list.sort()" encourages people to think of the sort() method as an inplace operation, which it wouldn't be on a view. "sorted()", on the other hand, already creates a new object. The only downside is that Py2.x & Py3k compatible code would look like: keys = sorted(d.keys()) which is likely to create the list *twice* in Py2.x. Cheers, Nick. -- Nick Coghlan | [EMAIL PROTECTED] | Brisbane, Australia --------------------------------------------------------------- http://www.boredomandlaziness.org _______________________________________________ Python-3000 mailing list Python-3000@python.org http://mail.python.org/mailman/listinfo/python-3000 Unsubscribe: http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com