Why does extend() fail in this case, and what am I doing wrong?

MRAB python at mrabarnett.plus.com
Tue Jul 14 11:13:43 EDT 2009


Xavier Ho wrote:
> Why doesn't the second output print [1, 2, 3, .... , 7, 8, 9] ?
> The code is run at: http://codepad.org/wgLU4JZh
> 
> class A():
>     def __init__(self):
>         self.n = [1, 2, 3, 4, 5]
>        
> a = A()
> print a.n
> print a.n.extend([6, 7, 8, 9])
> 
> #Output:
> #[1, 2, 3, 4, 5]
> #None
> 
> I really don't know, but I'm probably missing something. Any pointers 
> would be great.
> 
The extend() method modifies the list in-place and returns None. Try
printing a.n after extending.



More information about the Python-list mailing list