Uniform Function Call Syntax (UFCS)
Ian Kelly
ian.g.kelly at gmail.com
Sun Jun 8 12:54:42 EDT 2014
On Sun, Jun 8, 2014 at 10:24 AM, jongiddy <jongiddy at gmail.com> wrote:
> A contrived example - which of these is easier to understand?
>
> from base64 import b64encode
>
> # works now
> print(b64encode(str(min(map(int, f.readlines()), key=lambda n: n % 10)), b'?-'))
>
> # would work with UFCS
> f.readlines().map(int).min(key=lambda n: n % 10).str().b64encode(b'?-').print()
I prefer not making it a one-liner:
data = map(int, f.readlines())
min_data = min(data, key=lambda n: n % 10)
print(b64encode(str(smallest_data), b'?-'))
Python's standard of having in-place methods return None also forces
this to an extent. Whenever you want to tack on something like
.append(), that's the end of your chain and it's time to start a new
line anyway. Of course, you could always define something like:
def appended(iterable, x):
result = list(iterable)
result.append(x)
return result
and use that in your chain.
More information about the Python-list
mailing list