>From my experience teaching Python to non-programmers, it's a huge hurdle/nightmare to teach functions/methods that modify objects in-place vs. return a value that must be reassigned. Behold Pandas's DataFrame's sort method, which has an optional `in_place` argument that defaults to *False*, which despite being a method that looks like mylist.sort(), works differently from the method on lists, but more like the sorted *function*, arrrgh! That was a fun session...
I think for consistency, having object methods that can act in-place (semantics like mylist.pop, mylist.append are nice as Stéfane suggests) only act in-place, and functions return a new object for reassignment would help new users. Maybe I'm teaching it poorly, suggestions welcome. Nick On Wed, Mar 1, 2017 at 11:26 AM, Stéfane Fermigier <s...@fermigier.com> wrote: > Definitively not, just like M. Fowler: "Meyer likes to use command-query > separation absolutely, but there are exceptions. Popping a stack is a good > example of a query that modifies state. Meyer correctly says that you can > avoid having this method, but it is a useful idiom. So I prefer to follow > this principle when I can, but I'm prepared to break it to get my pop." > > What I wanted to point out is that the paragraph quoted by Stephan ("In > general in Python (and in all cases in the standard library) a method that > mutates an object will return None to help avoid getting the two types of > operations confused. So if you mistakenly write y.sort() thinking it will > give you a sorted copy of y, you’ll instead end up with None, which will > likely cause your program to generate an easily diagnosed error.") doesn't > seem to be true in this case. > > S. > > On Wed, Mar 1, 2017 at 6:23 PM, Cory Benfield <c...@lukasa.co.uk> wrote: > >> >> On 1 Mar 2017, at 10:26, Stéfane Fermigier <s...@fermigier.com> wrote: >> >> Cf. https://martinfowler.com/bliki/CommandQuerySeparation.html >> >> But: >> >> >>> l = [1,2,3] >> >>> l.pop() >> 3 >> >>> l >> [1, 2] >> >> => Not so true. >> >> S. >> >> >> This is naturally a different circumstance: pop must return the element >> it popped, otherwise it would just be del. Surely you aren’t suggesting >> that pop should return self? >> >> Cory >> > > > > -- > Stefane Fermigier - http://fermigier.com/ - http://twitter.com/sfermigier > - http://linkedin.com/in/sfermigier > Founder & CEO, Abilian - Enterprise Social Software - > http://www.abilian.com/ > Chairman, Free&OSS Group / Systematic Cluster - > http://www.gt-logiciel-libre.org/ > Co-Chairman, National Council for Free & Open Source Software (CNLL) - > http://cnll.fr/ > Founder & Organiser, PyData Paris - http://pydata.fr/ > --- > “You never change things by fighting the existing reality. To change > something, build a new model that makes the existing model obsolete.” — > R. Buckminster Fuller > > > > _______________________________________________ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ >
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/