It's a design decision that mutating methods do not return self. It's so
that you're forced to deal with them as mutators, and you don't
accidentally (or through ignorance) assume they return a new list.
If you want to work around this, you can either create helper functions,
or get more elaborate, such as this example:
On 8/13/10 11:00 PM, Jordan P wrote:
> hey nycpython,
> This is my first post to the group, though I've been a member for a while!
> I came across an interesting quirk in standard python regarding lists. I
> was in the middle of writing a beastly lambda expression and found
> myself having to insert a value at the beginning of a list. The design
> of my expression required that whatever call I made would have to return
> the list so that it could be used further up the chain.
> looking at the docs, I saw that list.insert(0, value) returns None, not
> the list.
> I was stuck for a good half hour until someone suggested I use: [val] +
> list instead. They warned, however, that this expression will cause a
> copy of list to be returned, not the original list. This stinks for
> Can anybody come up with a reason why python's list methods (append,
> insert, extend, etc..) do not return the list? I'm using [val]+list for
> now, but would much rather not be making copies of all my lists.
> Looking forward to some interesting discussion--
> Please Note: If you hit "*REPLY*", your message will be sent to
> *everyone* on this mailing list ([address removed]
> <mailto:[address removed]>)
> This message was sent by Jordan P ([address removed]) from The New
> York Python Meetup Group <http://www.meetup...;.
> To learn more about Jordan P, visit his/her member profile
> To unsubscribe or to update your mailing list settings, click here
> Meetup, PO Box 4668 #37895 New York, New York[masked] |
> [address removed]