[Python-ideas] Before and after the colon in funciton defs.
steve at pearwood.info
Thu Sep 22 19:58:32 CEST 2011
Eric Snow wrote:
> We're looking at two approaches here: do it in the function "header"
> or in the body. Here're some pros and cons:
There's a third approach, although it would probably take a bit more
implementation effort: a decorator.
# Demonstrate early-binding, micro-optimization and monkey-patching
# in a single call:
@inject(a=some_value(), len=len, sum=my_sum)
The downside is that the decorator gets called after the function is
already created, therefore it would have to take an existing function
and make a modified copy of it. This may be harder than doing the
"static" bindings while the function is being assembled in the first place.
> In-header Pros:
> - implies definition time
Likewise for the usual @ decorator syntax.
> In-header Cons:
> - limited space so more info adds clutter
> - could be mistaken as part of function's signature
Neither apply to decorator syntax.
> In-body Pros:
> - less cluttered
> - more closely associated with function locals?
Decorator is also less cluttered, but not so much obvious what it does.
> In-body Cons:
> - easy to miss that it's a definition-time directive (particularly if
> on RHS of assignment)
> - evaluated expression must not reference any of the function's locals
Neither of these is a problem for a decorator: using the @ syntax, it is
clearly and obviously definition-time. And because the inject decorator
is called outside the function, it's obvious that the bindings can't
access the function locals.
More information about the Python-ideas