copysort patch, was RE: [Python-Dev] inline sort option
Dan Aloni
da-x at gmx.net
Sat Oct 18 16:54:41 EDT 2003
On Sat, Oct 18, 2003 at 10:01:52PM +0200, Alex Martelli wrote:
>
> > >>> (lambda x:(x, x.sort())[0])(list(a))
>
> This one can, because the lambda lets you give a temporary name x
> to the otherwise-unnamed list returned by D.keys(). It can be made a
> _little_ better, too, I think:
>
> >>> D=dict.fromkeys('ciao')
> >>> D.keys()
> ['i', 'a', 'c', 'o']
> >>> (lambda x: x.sort() or x)(D.keys())
> ['a', 'c', 'i', 'o']
>
> and if you want it reversed after sorting,
>
> >>> (lambda x: x.sort() or x.reverse() or x)(D.keys())
> ['o', 'i', 'c', 'a']
Good, so this way the difference between copied and not
copied is minimized:
>>> (lambda x: x.sort() or x)(a)
And:
>>> (lambda x: x.sort() or x)(list(a))
Nice, this lambda hack is a cleaner, more specific, and simple
deviation of the chain() function.
Perhaps it could be made more understandable like:
>>> sorted = lambda x: x.sort() or x
>>> sorted(list(a))
['a', 'c', 'i', 'o']
And:
>>> sorted(a)
['a', 'c', 'i', 'o']
The only problem is that you assume .sort() always returns a non
True value. If some time in the future .sort() would return self,
your code would break and then the rightful usage would be:
>>> a = ['c', 'i', 'a', 'o']
>>> list(a).sort()
['a', 'c', 'i', 'o']
>>> a
['c', 'i', 'a', 'o']
And:
>>> a.sort()
['a', 'c', 'i', 'o']
>>> a
['a', 'c', 'i', 'o']
I didn't see the begining of this discussion, but it looks to me that
sort() returning self is much better than adding a .copysort().
> BTW, welcome to python-dev!
Thanks!
--
Dan Aloni
da-x at gmx.net
More information about the Python-Dev
mailing list