[Python-ideas] add fluent operator to everything

Dan Sommers 2QdxY4RzWzUUiLuE at potatochowder.com
Wed Feb 20 11:33:25 EST 2019


On 2/20/19 9:10 AM, Steven D'Aprano wrote:

 > That's possibly a matter of familiarity. I'd be very surprised if you
 > preferred this:
 >
 >      mystr = mystr.strip()
 >      mystr = mystr.expandtabs()
 >      mystr = mystr.lower()
 >      mystr = mystr.replace('ham', 'spam')
 >      result = function(mystr)
 >
 > over this fluent version:
 >
 >      result = 
function(mystr.strip().expandtabs().lower().replace('ham', 'spam'))

Those two blocks of code don't quite do the same thing.  What is the
value of mystr at the end of each block?  Yes, as a matter of fact, I do
think that that's the important question.

Consider these two similar blocks of code:

     f(mylist):
         mylist.sort()
         mylist.pop()
         mylist.reverse()

     g(mystr):
         return mystr.strip().expandtabs().replace('ham', 'spam')

The difference is more than a matter of which syntax I prefer, the
difference is a matter of how I build software.

Python lists and dicts are classic objects.  I don't interact with the
underlying data, I send the object a request to operate on that data,
and rest assured that the object is doing the right thing.  I can create
one at the top of my application, pass it around, make a series of
request against it, and ask that very same object for the end result:

     some_list = get_some_data()
     f(some_list)
     print(some_list) # what happened to my original?

Python strings, however, don't work that way.  If I create one at the
top of my application, I can pass it around, but my original remains as
is.

     some_string = get_some_data()
     another_string = g(some_string) # why do I need a new string?
     print(some_string)
     print(another_string)

It's a different way of doing things.  Please don't conflate them.

 > Or if you're worried about the line length:
 >
 >      result = function(mystr.strip()
 >                             .expandtabs()
 >                             .lower()
 >                             .replace('ham', 'spam')
 >                             )
 >
 > works for me.

I am no longer impressed that you squeezed the original into exactly 80
columns.  ;-)


More information about the Python-ideas mailing list