Language design

Markus Rother python at markusrother.de
Thu Sep 12 14:13:00 EDT 2013


On 12.09.2013 01:27, Chris Angelico wrote:
> On Thu, Sep 12, 2013 at 6:41 AM, Markus Rother <python at markusrother.de> wrote:
>>     3. The default return value of methods is None instead of self.
>>     If it was self, it would be possible to chain method calls (which
>>     is called a cascade in smalltalk).
>>
>>
>>     >>> lst = []
>>     >>> lst.append(1).append(2).append(3) ## FAIL
>>     Traceback (most recent call last):
>>     ...
>>     AttributeError: 'NoneType' object has no attribute 'append'
> 
> That's a policy decision: a method (or function) will *EITHER* return
> a value, *OR* mutate its primary argument (in the case of a method,
> that's self). 

You are stating: "All getters must be free of side effects".
That is not the case.  Furthermore, the demand for getters with hidden
side effects is the reasoning behind properties.

The policy could as well be: a method (not a function) will either
return a value, or return self, whether or not the object was mutated.

> Why should that be split into two statements? Or alternatively, why
> should an extra copy of the list be created (if you use Python's
> sorted() here)? But for the new programmer, this is a convenient
> safety-net, and if list.sort() worked the other way, it'd be just as
> much a gotcha ("I ask for a sorted list, and it also changed the
> original?!??").
I understand the point you are making in the end, in the interest of
having an easy to start with language.

Markus




More information about the Python-list mailing list