[Python-ideas] Attribute-Getter Syntax Proposal

Brett Cannon brett at python.org
Fri Mar 8 13:37:48 EST 2019


On Fri, Mar 8, 2019 at 8:57 AM Samuel Li <samuel.wgx at gmail.com> wrote:

> Don't know if this has been suggested before. Instead of writing something
> like
>
> >>> map(lambda x: x.upper(), ['a', 'b', 'c'])
>
> I suggest this syntax:
> >>> map(.upper(), ['a', 'b', 'c'])
>

Do note you get the same results with `map(str.upper, ['a', 'b', 'c'])`.


>
> This would also work for attributes:
> >>> map(.real, [1j, 2, 3+4j])
>
> Internally, this would require translating
>
> .attribute -> lambda x: x.attribute
>
> and
>
> .method(*args, **kwargs) -> lambda x: x.method(*args, **kwargs)
>
> This translation should only take place where a "normal" attribute lookup
> makes no sense (throws a SyntaxError); i.e. foo.bar works as before,
> foo(.bar) would previously throw a SyntaxError, so the new syntax applies
> and the .bar is interpreted as an attrgetter.
>
> This is of course only a cosmetic improvement over operator.attrgetter and
> operator.methodcaller, but I think it's nice enough to warrant
> consideration.
>
> If you like this idea or think it's utter garbage, feel free to discuss.
>

Sorry, I'm personally not a fan as it looks like you have a typo in your
code, e.g. you left of 'x' or something before the dot.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190308/3dde33e2/attachment.html>


More information about the Python-ideas mailing list