allow line break at operators

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Aug 15 15:30:26 CEST 2011


Seebs wrote:

> I tend to write stuff like
> 
> foo.array_of_things.sort.map { block }.join(", ")
> 
> I like this a lot more than
> array = foo.array_of_things
> sorted_array = array.sort()
> mapped_array = [block(x) for x in sorted_array]
> ", ".join(mapped_array)

If you insist on a one-liner for four separate operations, what's wrong with
this?

", ".join([block(x) for x in sorted(foo.array_of_things)])

Or if you prefer map:

", ".join(map(block, sorted(foo.array_of_things))


I think I would be less skeptical about fluent interfaces if they were
written more like Unix shell script pipelines instead of using attribute
access notation:

foo.array_of_things | sort | map block | join ", "



-- 
Steven




More information about the Python-list mailing list