[] and NoneType

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
Wed Aug 21 03:57:25 CEST 2002


----- Original Message ----- 
From: "beilin" <bzhang at sangamo.com>


> Hi,
> 
> Could anyone explain to me why [].append("test") returns None? What is
> the difference between the following two?
> >>> a = []
> >>> a.append("test")
> >>> print a
> ['test']
> >>>
> 
> and 
> 
> >>> a = [].append("test")
> >>> print a
> None
> >>>
> 
> I am confused.  Thanks

Sure.  You are thinking in Functional Programming terms; 
Python has only partial support for that.  In particular,
the .append method for lists does NOT return the list.
Ever.  It returns None.

Sorry.

This is a subject of frequent discussion around here, but
in the final analysis, that's how it is.

The first form you show is correct, the second incorrect.


Chris Gonnerman -- chris.gonnerman at newcenturycomputers.net
http://newcenturycomputers.net





More information about the Python-list mailing list