The Voidspace Techie Blog

Gravatar I haven't played with 2.6 yet, but named tuples look nice indeed. The 2.6 release notes say that "Several places in the standard library that returned tuples have been modified to return namedtuple instances". Do you know if attrgetter (when used for multiple attributes) also behaves like this in 2.6? (the docs don't mention it specifically, but it would seem an obvious place).


Gravatar Why do you think lambda are that bad? I personally find the property example `lambda self: self._document` more readable as it is more explicit than the eqivalent attrgetter('_document') as the argument seems to be not even to be an attribute as it is just a string.

On the other hand the second example (key=itemgetter(2)) removes the need for an extra variable name (item) which is not used just in the lambda anyway. Maybe as the lambda in the first example must use "self" the lambda there seems better and the 2nd example arbitrary name "item" makes itemgetter look better?


Gravatar (Guess I should preview my posts, please excuse and ignore all the rubbish typing and grammatics in my last post)


Gravatar Didn't you see my wink when I talked about eliminating lambdas?

Personally I find attrgetter and itemgetter cleaner and more readable.


Gravatar Seems attrgetter is also twice as fast as a lamda: http://pythonwise.blogspot.com/2...er-is- fast.html.


Gravatar The name "Schwartzian transform" seems to have an interesting history too.

Oh no! am I pointing to a "Lisp had it first" article

- Paddy.


Gravatar You itemgetter example has a bug - it should be itemgetter(1) (and the same for the lambda), since you only have two elements in the tuples and they are indexed from 0.

It is always a good idea to test code, even a two-line example in a blog .


Gravatar Thanks Dave.


Gravatar Use key instead of "decorate, sort, decorate" was Raymond Hettinger's first piece of advice in his PyCon UK code clinic.


Gravatar Yeah - I was there.

key is the DSU pattern built-in to Python, which is very cool.


Gravatar Python 2.6 also introduces a new function called methodcaller in the operator module.


Name:

Email:

URL:

Comment:  ? 

 

Commenting by HaloScan