[Tutor] Need Explanation...

Alan Gauld alan.gauld at btinternet.com
Sat Dec 10 18:04:19 CET 2011


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... :-)

> 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.

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



More information about the Tutor mailing list