[Python-ideas] Enhance definition of functions

Nick Coghlan ncoghlan at gmail.com
Fri Aug 2 17:46:37 CEST 2013


On 2 August 2013 23:55, Antoine Pitrou <solipsis at pitrou.net> wrote:
> Le Fri, 2 Aug 2013 22:48:33 +1000,
> Nick Coghlan <ncoghlan at gmail.com> a
> écrit :
>>
>> I would also tweak the early binding syntax to require an additional
>> keyword to make it read more like English:
>>
>>     seq = []
>>     for i in range(10):
>>         seq.append(?.f) given i=i in:
>>             def f(): return i
>>     assert [f() for f in seq] == list(range(10))
>
> I think any "inline function" proposal should focus on callback-based
> programming for its use cases.

I think callback based programming is a *good* use case, certainly,
but not the only one.

> In this context, you usually have one or
> two callbacks (two in Twisted-style programming: one for success, one
> for failure), passed positionally to a consuming function:
>
>     loop.create_connection((host, port), @cb, @eb) where:
>         def cb(sock):
>             # Do something with socket
>         def eb(exc):
>             logging.exception(
>                 "Failed connecting to %s:%s", host, port)

We can't use 'where' because we know it conflicts with the SQL sense
of the term in too many APIs. We're reasonably sure we can get away
with "given" without too much conflict, though.

Using "@" as the marker character is also problematic, since the
following degenerate case will probably confuse the parser (due to it
looking too much like a decorator clause):

    @something() given:
        ...

I liked the notion of "?" as suggesting doubt and uncertainty - an
element of "leave this undefined for now, we'll fill it in later".
While the out of order execution is related to decorators (hence @in
for PEP 403), I think PEP 3150 is more of a different notion,
especially with the revisions I suggested in this thread. I believe
your example still looks reasonable with the "?." notation for the
forward reference:

    loop.create_connection((host, port), ?.cb, ?.eb) given:
        def cb(sock):
            # Do something with socket
        def eb(exc):
            logging.exception(
                "Failed connecting to %s:%s", host, port)

Anyway, not something that's going to happen for 3.4, but a problem
I'm happy to keep chipping away at - some day we might find a proposed
solution that doesn't send Guido screaming in the other direction :)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list