[Python-ideas] Re: Shorthand syntax for lambda functions that have a single parameter

2021-09-29 Thread Chris Angelico
On Thu, Sep 30, 2021 at 4:19 PM Steven D'Aprano wrote: > > On Wed, Sep 29, 2021 at 02:09:03PM -0700, Guido van Rossum wrote: > > Over in typing-sig we're considering a new syntax for callable *types*, > > which would look like (int, int, str) -> float. A matching syntax for > > lambda would use a

[Python-ideas] Re: Shorthand syntax for lambda functions that have a single parameter

2021-09-29 Thread Steven D'Aprano
On Wed, Sep 29, 2021 at 02:09:03PM -0700, Guido van Rossum wrote: > Over in typing-sig we're considering a new syntax for callable *types*, > which would look like (int, int, str) -> float. A matching syntax for > lambda would use a different arrow, e.g. (x, y, z) => x+y+z. I like arrow operators

[Python-ideas] Re: Better exception handling hygiene

2021-09-29 Thread Soni L.
On 2021-09-29 11:46 p.m., Steven D'Aprano wrote: > In Soni's original code snippet, there is a clear separation of code > that is inside the try block from code that is outside the try block: > > > def a_potentially_recursive_function(some, args): > >   try: > > some.user_code() > > ex

[Python-ideas] Re: Better exception handling hygiene

2021-09-29 Thread Steven D'Aprano
In Soni's original code snippet, there is a clear separation of code that is inside the try block from code that is outside the try block: > def a_potentially_recursive_function(some, args): >   try: > some.user_code() > except ExceptionWeCareAbout as exc: > raise RuntimeError fro

[Python-ideas] Re: Better exception handling hygiene

2021-09-29 Thread Steven D'Aprano
On Wed, Sep 29, 2021 at 10:01:52PM -0300, Soni L. wrote: > So uh, this is a hardly at all fleshed out idea, but one thing we really > don't like about python is having to do stuff like this so as to not > swallow exceptions: Is that the Royal We or are you actually speaking on behalf of other pe

[Python-ideas] Re: Better exception handling hygiene

2021-09-29 Thread Soni L.
On 2021-09-29 10:09 p.m., Chris Angelico wrote: > On Thu, Sep 30, 2021 at 11:03 AM Soni L. wrote: > > > > So uh, this is a hardly at all fleshed out idea, but one thing we really > > don't like about python is having to do stuff like this so as to not > > swallow exceptions: > > > > def a_potent

[Python-ideas] Re: Better exception handling hygiene

2021-09-29 Thread Chris Angelico
On Thu, Sep 30, 2021 at 11:03 AM Soni L. wrote: > > So uh, this is a hardly at all fleshed out idea, but one thing we really > don't like about python is having to do stuff like this so as to not > swallow exceptions: > > def a_potentially_recursive_function(some, args): > """ > Does stuff and

[Python-ideas] Better exception handling hygiene

2021-09-29 Thread Soni L.
So uh, this is a hardly at all fleshed out idea, but one thing we really don't like about python is having to do stuff like this so as to not swallow exceptions: def a_potentially_recursive_function(some, args):   """   Does stuff and things.   Raises ExceptionWeCareAbout under so and so condition

[Python-ideas] Re: Shorthand syntax for lambda functions that have a single parameter

2021-09-29 Thread Guido van Rossum
Over in typing-sig we're considering a new syntax for callable *types*, which would look like (int, int, str) -> float. A matching syntax for lambda would use a different arrow, e.g. (x, y, z) => x+y+z. On Wed, Sep 29, 2021 at 11:51 AM Stephen J. Turnbull < stephenjturnb...@gmail.com> wrote: > Do

[Python-ideas] Shorthand syntax for lambda functions that have a single parameter

2021-09-29 Thread Stephen J. Turnbull
Dominik Vilsmeier writes: > Hence the proposal is to add a new syntax via the new token `?`. I find that unreadable. If you're going to add new syntax, either bite the bullet and just allow 'λ' for 'lambda' (my preference as an old lisper), or 'x → f(x)' (following MRAB's earlier suggestion).

[Python-ideas] Re: Shorthand syntax for lambda functions that have a single parameter

2021-09-29 Thread Dominik Vilsmeier
Chris Angelico wrote: > On Wed, Sep 29, 2021 at 10:56 PM Dominik Vilsmeier > dominik.vilsme...@gmx.de wrote: > > members.sort(key=(?[1], ?[0])) > > How do you know whether this is one function that returns a tuple, or > a tuple of two functions? > ChrisA You are right, I didn't think of this ambig

[Python-ideas] Re: Shorthand syntax for lambda functions that have a single parameter

2021-09-29 Thread MRAB
On 2021-09-29 10:11, Dominik Vilsmeier wrote: Lambda functions that have a single parameter are a common thing, e.g. for "key" functions: `sorted(items, key=lambda x: x['key'])`. For these cases however, the rather long word "lambda" together with the repetition of the parameter name, results i

[Python-ideas] Re: Shorthand syntax for lambda functions that have a single parameter

2021-09-29 Thread Christopher Barker
Given that we have comprehensions that use s simple expression, and operator.itemgetter for common keys, the use cases for these simple lambdas are pretty rare these days. Sure, some folks seem to prefer map and filter as a matter of style, but I don’t think we need to create cryptic notation to m

[Python-ideas] Re: Shorthand syntax for lambda functions that have a single parameter

2021-09-29 Thread Steven D'Aprano
On Wed, Sep 29, 2021 at 09:11:35AM -, Dominik Vilsmeier wrote: > * `sorted(items, key=?['key'])` > * `filter(? > 0, items)` > * `map(f'{?:.3f}', items)` I think those are massively more cryptic and hard to read than an explicit lambda. Being too terse is worse that being too verbose: consid

[Python-ideas] Re: Shorthand syntax for lambda functions that have a single parameter

2021-09-29 Thread Dennis Sweeney
IIUC a lot of what is being discussed has been implemented by the "placeholder" package on PyPI Here: https://pypi.org/project/placeholder/ It allows using things like `min(data, key=_[-1])` or `_.age < 18` (just using language features instead of adding new syntax). ___

[Python-ideas] Re: Shorthand syntax for lambda functions that have a single parameter

2021-09-29 Thread Damian Shaw
I find this approach too cryptic compared to reading regular Python notation, my brain has to mode switch to make sense of it. Would a little extra ?: be too much add to make clear it's a lambda function, e.g. ?: ? > 0 instead of ? > 0 Also either approach *could *add multi-argument lambdas: ?1, ?

[Python-ideas] Re: Shorthand syntax for lambda functions that have a single parameter

2021-09-29 Thread Ricky Teachey
Will we be able to splat/unpack the `?`? >>> args = get_starting_list_values() >>> args (1, 2, 3) >>> dd = defaultdict([*?]) >>> dd["spam"] [1, 2, 3] or: >>> kwargs = get_kwargs() >>> kwargs {'x': 1, 'y' 2, 'z': 3} >>> dd = defaultdict(Node(**?)) >>> dd["eggs"] Node(x=1, y=2, z=3) --- Ricky. "

[Python-ideas] Re: Shorthand syntax for lambda functions that have a single parameter

2021-09-29 Thread Chris Angelico
On Wed, Sep 29, 2021 at 10:56 PM Dominik Vilsmeier wrote: > members.sort(key=(?[1], ?[0])) How do you know whether this is one function that returns a tuple, or a tuple of two functions? ChrisA ___ Python-ideas mailing list -- python-ideas@python.org T

[Python-ideas] Shorthand syntax for lambda functions that have a single parameter

2021-09-29 Thread Dominik Vilsmeier
Lambda functions that have a single parameter are a common thing, e.g. for "key" functions: `sorted(items, key=lambda x: x['key'])`. For these cases however, the rather long word "lambda" together with the repetition of the parameter name, results in more overhead than actual content (the `x['ke