[Tutor] decomposing a problem

Alan Gauld alan.gauld at yahoo.co.uk
Tue Dec 25 20:06:04 EST 2018


On 26/12/2018 00:00, Avi Gross wrote:

> great. Many things in python can be made to fit and some need work. Dumb
> example is that sorting something internally returns None and not the object
> itself. 

This is one of my few complaints about Python.
In Smalltalk the default return value from
any method is self. In Python it is None.

self allows chaining of methods, None does not.
Introducing features like reversed() and sorted()
partially addresses the issue but leads to
inconsistent and ugly syntax.

Smalltalk uses this technique so much it has
its own code layout idiom (Pythonised as
follows):

object
   .method1()
   .method2()
   .method3()
   ....
   .lastone()

We can do this with some methods but not all.
And of course methods that return a different
type of value require careful handling (eg. an
index() call in the middle of a set of list
operations means the subsequent methods are
being called on an int not a list - which if
handled correctly can be confusing and if
not handled correctly produces errors! (The
idiomatic way says don't chain with methods
not returning self!)

In practice I (and the Smalltalk community) don't
find that an issue in real world usage, but it
may have been why Guido chose not to do it that way.
But I still curse the decision every time I hit it!

But as I said, it's about the only thing in Python
I dislike... a small price to pay.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list