list.append(x) could return x
Hi all, Currently, list.append(x) mutates the list and returns None. It would be a little syntactic sugar to return x, for example: something = mylist.append(Something()) What do you think ? Thanks in advance for your replies -- ∞
It is a standard convention in Python that mutating methods return None. While that does make chaining operations harder (impossible), it is a consistent convention that makes it much harder to get confused about whether a method mutates or not. It is not going to change. See previous threads about a “fluent” interface for discussion about the concept. -CHB On Mon, Apr 20, 2020 at 8:42 AM J. Pic <jpic@yourlabs.org> wrote:
Hi all,
Currently, list.append(x) mutates the list and returns None.
It would be a little syntactic sugar to return x, for example:
something = mylist.append(Something())
What do you think ?
Thanks in advance for your replies
-- ∞ _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/3RHQB6... Code of Conduct: http://python.org/psf/codeofconduct/
-- Christopher Barker, PhD Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython
On 20/04/2020 16:57, Christopher Barker wrote:
It is a standard convention in Python that mutating methods return None.
While that does make chaining operations harder (impossible), it is a consistent convention that makes it much harder to get confused about whether a method mutates or not.
It is not going to change.
See previous threads about a “fluent” interface for discussion about the concept.
I think you may have made the same mistake I initially did. The OP isn't asking for the mutated list to be returned, they are asking for the object added. Which pretty much proves my point about it being really unexpected ;-)
On Mon, Apr 20, 2020 at 8:42 AM J. Pic <jpic@yourlabs.org> wrote:
Hi all,
Currently, list.append(x) mutates the list and returns None.
It would be a little syntactic sugar to return x, for example:
something = mylist.append(Something())
What do you think ?
Thanks in advance for your replies
-- ∞
-- Rhodri James *-* Kynesim Ltd
On 20/04/2020 16:25, J. Pic wrote:
Hi all,
Currently, list.append(x) mutates the list and returns None.
It would be a little syntactic sugar to return x, for example:
something = mylist.append(Something())
What do you think ?
I'm generally opposed to trying to do too many things on one line; the more you pile in, the more chance it will be misunderstood. In this case, why would the average naive user expect that your new Something is what's going to be returned? The usual request is to have (the mutated) `mylist` returned (which we don't do for reasons), and that's what people are going to expect. -- Rhodri James *-* Kynesim Ltd
On 4/20/20 11:25 AM, J. Pic wrote:
Hi all,
Currently, list.append(x) mutates the list and returns None.
It would be a little syntactic sugar to return x, for example:
something = mylist.append(Something())
What do you think ?
Now you can do this (but it could be unpopular): mylist.append(something := Something()) --Ned.
On 4/20/20 11:25 AM, J. Pic wrote:
Hi all,
Currently, list.append(x) mutates the list and returns None.
It would be a little syntactic sugar to return x, for example:
something = mylist.append(Something())
What do you think ?
Thanks in advance for your replies
I think the main idea of returning None is that if you started to do: something = mylist.append(foo) Then you might be tempted to forget that mylist got changed. Fairly consistently Python makes mutating members return None, and non-mutating members return the result, so it is clear which is which. Yes, it says that you can't do mylist.append(foo).append(bar) but that really isn't isn't that bad. -- Richard Damon
On Apr 20, 2020, at 08:41, J. Pic <jpic@yourlabs.org> wrote:
Currently, list.append(x) mutates the list and returns None.
Yes, which is consistent with the vast majority of mutating methods in Python. It would be pretty weird to make lst.append(x) return x, while lst.extend(xs) still returns None, not to mention similar methods on other types like set.add.
It would be a little syntactic sugar to return x, for example:
something = mylist.append(Something())
You can already get the same effect with: mylist.append(something := Something()) I think usually this would be more readable (and pythonic) as two lines rather than combined into one: something = Something() mylist.append(something) But “usually” isn’t “always”, and that’s why we have escape hatches like the walrus operator. It’s for exactly this purpose, where a subexpression needs to be bound to a name, but for some reason it can’t or shouldn’t be extracted to a separate assignment statement. And I think this is a lot clearer about what’s getting assigned to something, too. Someone who’s never seen the walrus operator won’t understand it, but at least it’s unlikely they’re going to misunderstand it as something other than what it means. This is especially an issue with methods like list.append. In a lot of other languages, they return self, because the language encourages method chaining for fluid programming (Perl, Ruby), or because list appending is non-mutating (Scala, Haskell), or for bizarre reasons specific to that language (Go), so a lot of people are likely to misunderstand your syntax as meaning that something gets the new value of the list.
On 4/20/2020 9:25 AM, J. Pic wrote:
Hi all,
Currently, list.append(x) mutates the list and returns None.
It would be a little syntactic sugar to return x, for example:
something = mylist.append(Something())
What do you think ?
Thanks in advance for your replies
The following is not currently possible, but is consistent with other functions such as 'sorted': something = mylist.appended(Something()) -gyro
On Mon, Apr 20, 2020 at 10:07 AM gyro funch <gyromagnetic@gmail.com> wrote:
On 4/20/2020 9:25 AM, J. Pic wrote: The following is not currently possible, but is consistent with other functions such as 'sorted':
something = mylist.appended(Something())
no, it's not -- sorted() is a function, not a method. and there is a reason it's not list.sorted() (more than one actually) It turns out the OP wan't asking for exactly what I thought, but the point still stands: mutating methods return None. That is a consistent interface standard in Python. But your suggestion of an .appended() method does tie in to the "fluid interface" conversation on this list a while back -- if you are interested in the idea, I suggest you search the archives. -CHB
-gyro _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/JH3F46... Code of Conduct: http://python.org/psf/codeofconduct/
-- Christopher Barker, PhD Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython
On 4/20/2020 11:18 AM, Christopher Barker wrote:
On Mon, Apr 20, 2020 at 10:07 AM gyro funch <gyromagnetic@gmail.com <mailto:gyromagnetic@gmail.com>> wrote:
On 4/20/2020 9:25 AM, J. Pic wrote: The following is not currently possible, but is consistent with other functions such as 'sorted':
something = mylist.appended(Something())
no, it's not -- sorted() is a function, not a method. and there is a reason it's not list.sorted() (more than one actually)
It turns out the OP wan't asking for exactly what I thought, but the point still stands: mutating methods return None. That is a consistent interface standard in Python.
But your suggestion of an .appended() method does tie in to the "fluid interface" conversation on this list a while back -- if you are interested in the idea, I suggest you search the archives.
-CHB
Yes, I know the difference between a function and method and understand the motivation for the function 'sorted' versus a method. The idea was that there was a strong enough need in the past to include another 'tense' of sort that returned a transformed list from the input iterable. I presume that list.appended could do an analogous thing. I do, however, appreciate the pointer to the 'fluid interface' conversation.
TBH I had read about mutating methods returning None, and the fluent thread, I just didn't make the relation. Thanks Ned for the assignment operator reminder, that solves my use case perfectly. Have a great day Also, I love you <3
participants (7)
-
Andrew Barnert
-
Christopher Barker
-
gyro funch
-
J. Pic
-
Ned Batchelder
-
Rhodri James
-
Richard Damon