[Python-ideas] Enhance definition of functions

Antoine Pitrou solipsis at pitrou.net
Fri Aug 2 18:00:48 CEST 2013


Le Sat, 3 Aug 2013 01:46:37 +1000,
Nick Coghlan <ncoghlan at gmail.com> a
écrit :
> 
> > 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.

How about reusing "with"? There's no ambiguity with context managers
since the syntactic context is different.

> 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:
>         ...

No, that would simply be forbidden. In this proposal, "@" can only mark
names of parameters in function calls. We already reuse "*" and "**"
for a specific meaning in front of function call parameters, so there's
a precedent for such polysemy.

> I liked the notion of "?" as suggesting doubt and uncertainty - an
> element of "leave this undefined for now, we'll fill it in later".

I don't really like it :-) "?" has other meanings traditionally: as part
of the ternary operator in C-like languages (many of them), as a
wildcard character in pattern matching languages, as a marker of
optional matchers in regular expressions.

Also, I really don't like the idea that "?" represents a full-blown
object with attribute access capabilities and whatnot. It smells too
much like Perl-style (Ruby-style?) magic variables. My proposal is more
limited: it's a syntactic addition, but it doesn't create new runtime
objects or types.

Regards

Antoine.




More information about the Python-ideas mailing list