[Tutor] List methods/comps Best Practices

Kent Johnson kent37 at tds.net
Mon Apr 3 20:57:07 CEST 2006


stv wrote:
>   # return all changes, deletes first
>   return dels.extend(adds)
> 
> Since extend returns None, I ran into a lot of not-iterable errors
> when calling this code. So I fixed this with
> 
>   dels.extend(adds)
>   return dels
> 
> And all is good, although it took way more head scratching than typing
> . (In writing this, I now vaguely remember one of my tutorials warning
> me about something ... maybe this was that? :)
> 
> So my question is this: Is this just one of those things that I learn
> the hard way in Pthon & that's it, or am I doing something weird? 

In general, mutating methods return None. For example list.append(), 
list.sort(), list.extend() all return None. This is an intentional 
design choice in Python. The idea is it keeps you from mistakenly 
mutating an object.

Kent



More information about the Tutor mailing list