Python vs. Ruby

Mike C. Fletcher mcfletch at
Wed Jan 1 05:51:10 CET 2003

Justin Shaw wrote:

>>    * breaking object-orientation to return new objects when sorting
>>      sequences ;)
>>          o might also want to explain that it's not a hard thing to fix
>>            at some point in Python (if the powers-that-be want to) by
>>            adding "sorted" and "reversed" methods to the list object
>>            (no clue why you want "append" to create a new list, sounds
>>            silly to me ;) ).
>You don't have to return a "new" list.  What is non-object oriented about
"motivation" ;) .  Oh, and "encapsulation" ;) .

The idea of object orientation is (roughly):
    state with bound methods which modify that state
    encapsulation of operation such that individual methods/objects have 
obvious and minimal functionality, without concern for particular 
use-cases not in the contract/semantics of the particular method/object

As I mentioned, I personally would prefer to go for a less pure OO 
approach, but the discussion is whether returning self is OO.  Returning 
a new list is nicely OO in character. OO is littered with "create a new 
object just like this one save for this aspect" methods, so:

    t = s.sorted()

makes sense (you get back a new, sorted version of s from this method of 
s). The fact that it fits nicely into functional style programming is 
gravy.  The method does exactly what it sounds like it should do (create 
a sorted version).  When you do:

    s = s.sort()

which should sort return, the original or a copy?  

Why (from an OO perspective, I fully understand the desirability from a 
functional perspective) would it return a pointer to something you 
already have a pointer to when the function has nothing to do 
semantically with "get reference to object"?  Purity says you're sorting 
the object, not sorting the object and then returning random pieces of 
state (such as the object reference (why not return a pointer to the 
bound reverse method instead, since I often call that after calling sort 
;) ?)).

But, in the end, really, who cares ;) :) .  This thread is _by far_ the 
most thought I've spent on this "issue" in the last year and a half of 
coding Python 8hrs/day.  Adding sorted() and reversed() to the built-in 
class would probably be doable if people cared. But who knows, maybe 
Guido will get a visit from a functional-programming-mafia type, reverse 
his decision, and allow returning "self".

It's a whole new year (well, in 9 minutes), after all,

  Mike C. Fletcher
  Designer, VR Plumber, Coder

More information about the Python-list mailing list