[Tutor] Keeping change-in-place vs. copy methods straight

Alan Gauld alan.gauld at btinternet.com
Mon May 5 21:04:56 CEST 2014


On 04/05/14 13:54, Dave Angel wrote:
> Alan Gauld <alan.gauld at btinternet.com> Wrote in message:

>> I assumed (never assume!) that it returned a reference to the original.
>> I really, really, hate the way Python handles this :-(
>
> It's not clear to me what you would change. Would you only provide
>   methods (like sort) that mangle their object?

No, I'd not provide methods that work on the object and return None.
I much prefer the Smalltalk model where if all else fails you return 
self. Since Python has objects everywhere they could easily have adopted 
that model, it is so much more consistent. And it also allows method 
chaining... But just the consistency of always getting a useful
object (I don't mean None!)  back makes a big difference to programming.

It does mean you need to make copy semantics obvious in the naming
but that's not too onerous. So sorted() would become sortedCopy() or 
some such.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list