On Dec 10, 2011, at 12:04 PM, Alan Gauld wrote: > On 10/12/11 16:46, Steven D'Aprano wrote: > >> ...the alternative would also have caught out everybody at some point. >> Consider a hypothetical Python where mutator methods returned a result: >> >> a = [1, 2, 3] >> b = a.append(4) >> >> Does this mean...? >> >> * append 4 to a, then return a (and therefore a and b are >> alternative names for the same list) > > This is what I'd expect. > I'm thinking about the Smalltalk model where the default return > value from a method is self... > > I'm particularly sensitive to this just now because I'm playing > with Squeak (again) and the elegance and consistency of > Smalltalk's mechanism stands in stark contrast to the mixed > model in Python. (OTOH Smalltalk overall is a frustrating > experience for me, I would like to love it but never quite > get there... :-)
Personally, I found that returning a copy of a seemed more logical- after all, if you return 4 to b, then adding 2 to b wouldn't make 4 equal 6. > >> circumstances, regardless of which behaviour was choosen for append, it >> would catch out some people some time. > > Probably, although if returning 'self' were the default (which > of course only makes sense in a pure OO world like Smalltalk) people would > get used to the semantics. Consistency is all in these kinds of situations. > Sadly its one of the few areas where Python is slightly inconsistent. > >> A better alternative would be for Python to have procedures as well as >> functions/methods, so that: >> >> b = a.append(4) >> >> would raise an exception immediately. > > Better than silently returning None for sure. Of course, by silently returning None, you can just go on with your daily life and be happily ignorant of any return value; in other more strongly typed languages, the void functions/methods tend to alter other variables and situations more than, for example, ints. I feel myself that it is no more trouble to simply type 'a.append(4); b = a'. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor