Why doesn't this work?

Tim Peters tim.one at home.com
Thu Dec 6 23:23:42 EST 2001


[Courageous]
> Python 2.2b2 (#26, Nov 16 2001, 11:44:11) [MSC 32 bit (Intel)] on
> win32
> Type "copyright", "credits" or "license" for more information.
> IDLE 0.8 -- press F1 for help
> >>> def f(): return [1,2,3]
>
> >>> f()
> [1, 2, 3]
> >>> f().append(4)
> >>> l=f().append(4)
> >>> l
> >>>
>
> Obviously I can fix this, but I'm wondering why it doesn't work
> the way I expected it to?

You didn't say what you expected, so how should we know <wink>?

list.append returns None.  In general, most mutate-in-place methods of
mutable objects return None (list.append, list.remove, list.sort,
list.reverse, dict.update, ...).  If they didn't, people would gripe about

x = [1, 2, 3]
y = x.append(4)

instead.  "How come got 4 stuffed at the end of x too?"





More information about the Python-list mailing list