[Python-ideas] Attribute-Getter Syntax Proposal

Steven D'Aprano steve at pearwood.info
Fri Mar 8 23:48:36 EST 2019


On Sat, Mar 09, 2019 at 05:19:50PM +1300, Greg Ewing wrote:
> If we were going to add a syntax for abbreviating lambdas, I would
> rather see something more generally useful, e.g.
> 
>    x -> x.method()
> 
> as an abbrevation for
> 
>    lambda x: x.method()

Cocnut does this!

https://coconut.readthedocs.io/en/latest/DOCS.html#lambdas

It allows any arbitrary expression and parameter list, rather 
than being limited to a single special case:

    lambda x, y=None, *args, **kw: spam(x)+y.eggs()-len(args)+kw['foo']

    (x, y=None, *args, **kw) -> spam(x)+y.eggs()-len(args)+kw['foo']
    # Saves an entire three columns! *wink*


(I believe this is similar to Haskell's syntax.)

Given that we have lambda though, and many people don't like anonymous 
functions, I doubt there's enough advantage to justify adding new 
syntax.

I prefer the look of -> to lambda, but given that we have lambda already 
I wouldn't want to waste that nice looking arrow operator on something we 
already can do. I'd rather see it saved for something more interesting, 
like a hypothetical cascading (fluent) method call syntax, or pattern 
matching.


-- 
Steven


More information about the Python-ideas mailing list