LangWart: Method congestion from mutate multiplicty

Rick Johnson rantingrickjohnson at gmail.com
Sun Feb 10 19:25:48 EST 2013


On Sunday, February 10, 2013 7:30:00 AM UTC-6, Oscar Benjamin wrote:
> On 10 February 2013 04:53, Mark Janssen wrote:
> > [...]
> > I have to agree with Rick, I think requiring the user to explicitly
> > create a new object, which is already a good and widely-used practice,
> > should be the Only One Way to Do It.
> 
> Why should I copy a potentially large data structure just to iterate
> over it in reverse order? 

That's a good question, and the answer is: "Only a fool would make a copy of ANY data structure only to simply iterate over it; be it forwards or backwards or sideways". 

> And why on earth would you want to remove
> the more efficient ways of doing this?

Well these "ways"(sic) might be more efficient, at this time, because the Python developers have defined them to be. This could be changed if they would drop the aversion to true OOP paradigm.

To make this work properly you would need to optimize the constructor of the sequence object. If the user presents the seq object (say for example a list) with variable that points to an already existing list like:

py> a = [1,2,3]
py> for x in list(a).reverse():
...     do_something

Python will not actually create a copy of the list because that would be foolish! Python would instead ITERATE over the existing object transparently. It's called OPTIMIZING CODE! By doing this we gain many things:

  * We don't have these foolish "mutate"<->"copy mutate"
    method "pairs" like: "seq.reverse()" and
    "seq.reversed()"
  
  * We are writing maintainable code by explicitly calling
    "Sequence(seq).mutate()". The intent becomes obvious
    even though Python may "optimize" our intentions "behind
    the scenes".
  
> > Guessing method names is far suboptimal to this simple, easy idiom.
> 
> There is no guessing. If the object has a __reverse__ method then it
> specifically advertises that it knows how to create an iterator that
> gives its values in reverse order. Otherwise __len__ and __getitem__
> are used.

Really. 

And you know that simply from intuiting a seemingly unrelated method? Wow, i'd bet the detectives of many municipalities would love to rent some of your powers. What sort of esoteric rule book are you reading from my friend?



More information about the Python-list mailing list