Why return None?

Alex Martelli aleaxit at yahoo.com
Thu Aug 26 04:35:10 EDT 2004


Martin DeMello <martindemello at yahoo.com> wrote:

> Ayose <ayose.cazorla at hispalinux.es> wrote:
> > 
> > If you can use python 2.3 or newer, try with [::-1]
> > 
> >     >>> def f(m, n):
> >     ...     print (m + n)[::-1]
> >     ... 
> >     >>> f([1,2,3], [10,20,30])
> >     [30, 20, 10, 3, 2, 1]
> 
> Thanks! Where can I look this up?

There should be recipes for such handy shortcuts in the Python Cookbook
(the current printed edition doesn't have this one as it only covers
Python 1.5.2 to 2.2, but we're preparing a second edition focusing on
Python 2.3 and 2.4).

In Python 2.4, by the way, reversed(x) [[using the new built-in function
'reversed']] is most often preferable to x[::-1].  reversed returns an
iterator, optimized for looping on, but if you need a list, tuple, etc,
you can just call list(reversed(x)) and so on.


Alex



More information about the Python-list mailing list