[Python-ideas] Re: Pattern matching in python function headers

2021-08-06 Thread Sam Frances
All good points. To me there is an elegance in being able to split the
different paths of the function into their own syntactically separate
function definitions.

One place where this excels is in handling happy paths vs error paths.
It increases clarity substantially, in my experience, to be able to code
the happy path without separately from the error path.

```
def foo("error")
# do some error handling

def foo(n):
# code the happy path
```

Whether this is worth the extra syntax, as you put it, I'm not sure.

I don't think I have the knowledge to write a PEP that has a reasonable
chance of adoption, so at this point I'm just "offering the idea".

On 05/08/2021 10:24, Paul Moore wrote:
> I assume that such a feature would simply behave exactly the same as a
> match statement:
> 
> def fib(arg):
> match arg:
> case 0:
> return 0
> case 1:
> return 1
> case n:
> return fib(n-1) + fib(n-2)
> # I know this is only a trivial example,
> # but you should probably also handle
> # n < 0. Does your proposal cover using
> # guards, or would they need a conditional
> # in the final case?
> 
> def allow_entry(arg):
> match arg:
> case {"name": "Bob"}:
> return "Bob is not allowed in ever!"
> case {"name": "Charles", "day": "Tuesday"}:
> return "It's a Tuesday, so Charles is allowed in."
> case {"name": "Charles", "day": _}:
> return "Charles is only allowed in on a Tuesday."
> case {"name": name}:
> return f"Come in {name}, make yourself at home!"
> # I only skimmed the PEP, and I don't have a copy of Python 3.10 to check,
> # but I believe that if arg doesn't match any of the given
> clauses, the match
> # silently does nothing. I don't know if that was your expected
> behaviour for
> # the definition form, but you should probably be explicit about
> what you intend
> # in any case.
> 
> Note that I created these by taking your examples and applying a
> purely mechanical translation - I didn't think about it at all, so
> this transformation could easily be applied mechanically. What's the
> benefit that would justify having this additional syntax?
> 
> In addition, you'd need to consider the implications of possibilities
> you didn't cover in your examples. I don't know Elixir, so I can't say
> whether that language has similar scenarios that we could follow, but
> these are all valid in Python, so we need to consider them.
> 
> Python's function definitions are executed at runtime, so you need to
> decide what behaviour you want from a function definition that's had
> *some* of its parts executed, but not others. So how would the
> following behave?
> 
> def example(1):
> return "One"
> 
> print(example(2))
> 
> def example(2):
> return "Two"
> 
> print(example(2))
> 
> Worse, what if the example(2) definition were in a separate module?
> 
> What about decorators?
> 
> def example(1):
> return "One"
> 
> @some_decorator
> def example(2):
> return "Two"
> 
> That's just a very brief list of "things to think about". In
> functional languages, I like this style of function definition, but it
> would need some fairly careful design to be able to translate it to a
> language like Python. And honestly, I'm not sure I see the advantage
> (in Python).
> 
> I'm not against the suggestion as such, but I think it will need a
> fair amount of work to flesh it out from a series of basic examples to
> a full-fledged proposal (which even then might end up getting
> rejected). Is that something you're considering doing yourself, or are
> you just offering the idea in case someone else is interested in
> picking it up?
> 
> Paul
> 
> On Thu, 5 Aug 2021 at 09:52, Sam Frances  wrote:
>>
>> Following on from PEP 634, it would be good to be able to have Erlang /
>> Elixir-style pattern matching in function headers.
>>
>> (I don't know what the technical term is for this language feature, but
>> hopefully the examples will clarify.)
>>
>> Here's the obligatory fibonacci example:
>>
>> ```
>> def fib(0):
>> return 0
>>
>> def fib(1):
>> return 1
>>
>> def fib(n):
>> return fib(n-1) + fib(n-2)
>> ```
>>
>> Or, another example:
>>
>> ```
>> def allow_entry({"name": "Bob&

[Python-ideas] Pattern matching in python function headers

2021-08-05 Thread Sam Frances
Following on from PEP 634, it would be good to be able to have Erlang /
Elixir-style pattern matching in function headers.

(I don't know what the technical term is for this language feature, but
hopefully the examples will clarify.)

Here's the obligatory fibonacci example:

```
def fib(0):
return 0

def fib(1):
return 1

def fib(n):
return fib(n-1) + fib(n-2)
```

Or, another example:

```
def allow_entry({"name": "Bob"}):
return "Bob is not allowed in ever!"

def allow_entry({"name": "Charles", "day": "Tuesday"}):
return "It's a Tuesday, so Charles is allowed in."

def allow_entry({"name": "Charles", "day": _}):
return "Charles is only allowed in on a Tuesday."

def allow_entry({"name": name}):
return f"Come in {name}, make yourself at home!"
```

Thanks for considering my idea.

Kind regards

Sam Frances
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/OPLBLWJPCN66QRUQNHBQSQOBNBFZRRBF/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Basic toolkit for async iterators

2021-03-07 Thread Sam Frances
Apologies if this has already been raised at some point, but I wanted to
raise the idea of a basic standard library toolkit for operations on
async iterators.

This would include versions of functions that are already in the
standard library for non-async iterators, such as *map*, *filter*,*zip
*etc. It could also include a *merge* function for async iterators, for
example (this would yield items in whatever order they are yielded from
from the constituent iterators).

There are, of course, open source libraries providing these functions.
But in general there is far too wide a range of such libraries, and it
is difficult to tell which ones are best maintained. For the most basic
and universally useful of operations on async iterators, it would be far
better if they were part of the standard library.

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/JAVMCP75ZQO5JMC2LDSCL24BOYJ7K5ZX/
Code of Conduct: http://python.org/psf/codeofconduct/