[Python-ideas] Before and after the colon in funciton defs.
Guido van Rossum
guido at python.org
Wed Sep 21 20:34:17 CEST 2011
When you say "compile time", do you literally mean "when the compiler
creates the bytecode" or do you really intend this to be computed at
function definition time (the same time when default values are
evaluated)? The latter seems to make more sense.
On Wed, Sep 21, 2011 at 11:12 AM, Ron Adam <ron3200 at gmail.com> wrote:
> 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
>> return impl
>> This would be used as follows:
>> 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
>> >>> adder(1)
>> >>> adder(5)
>> >>> adder(10)
>> >>> adder.call_count
> 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)
> Python-ideas mailing list
> Python-ideas at python.org
--Guido van Rossum (python.org/~guido)
More information about the Python-ideas