[Python-ideas] Before and after the colon in funciton defs.

Ron Adam ron3200 at gmail.com
Wed Sep 21 20:12:27 CEST 2011


On Wed, 2011-09-21 at 15:19 +1000, Nick Coghlan wrote:

> Perhaps a non-syntax way to approach both of these would be to add a
> new decorator to 'functools':
> 
>     def closure(f):
>         """Invokes the decorated function and returns the result after
> transcribing essential function metadata
> 
>            This can be used to easily share algorithm state and get
> early binding semantics for names.
>         """
>         impl = f()
>         impl.__name__ = f.__name__
>         doc = f.__doc__
>         if doc is not None:
>             impl.__doc__ = doc
>         impl.__dict__.update(f.__dict__)
>         return impl
> 
> This would be used as follows:
> 
>     @functools.closure
>     def adder(i=i): # 'impl' defines call time signature
>         "Increments 'x' by adder.value"
>         def impl(x):
>             impl.call_count += 1
>             return x + i
>         impl.value = i
>         impl.call_count = 0
>         return impl
> 
> >>> adder.value
> 10
> >>> adder(1)
> 11
> >>> adder(5)
> 15
> >>> adder(10)
> 20
> >>> adder.call_count
> 3

Simplifying things like this is one of the use cases of allowing define
time statements.  That's a lot of work to just avoid putting a keyword
in the signature.  And it's not easy to understand.

Decorators could be a good way to do this, but the problem in these
cases, is the function object doesn't have the needed support to make
things like this easy.


Probably the easiest and most direct way, would to be to add a new
keyword 'static' as MRAB suggested, but have it be an expression instead
of a command.

value = (static <expression>)


def adder(x):
    return x + (static i)    # evaluate (static i) at compile time.

The parentheses would be optional.

The (static i) expression, could be spelled (i=i).  I think that was
what Guido was suggesting, but not as an expression.  As an expression,
you would then see things like.. i = (i=i).  But that may only be poor
style, because it's easy enough to just not do that.

I think the expression form is better myself, it allows you to get both
the compile time value, and the current value of an identifier.


def value elapsed_frames():
    """ Where f is the frame counter in the parent scope. """
    return f - (static f)


Cheers,
   Ron




More information about the Python-ideas mailing list