Code evaluation at function definition execution time (was Re: Compile time evaluation (aka eliminating default argument hacks))

Nick Coghlan ncoghlan at iinet.net.au
Fri Feb 25 20:34:30 EST 2005


Steven Bethard wrote:
> So just to clarify, the issue you're trying to address is when you want 
> early binding like function default arguments get, but you don't want to 
> declare the names as function arguments?

Basically, yeah. Although I later realised I got the name of the feature I want 
wrong - default arguments are evaluated when the def statement is executed, not 
when the code is compiled. So it's a matter of being able to execute some code 
in the functions local namespace at compile time.

I have my doubts about the feasibility of the syntax I posted, too. A version 
which the Python compiler and parser should actually be able to handle would be 
to tack an optional clause on to the front of the function definition statement:

using x, y from:
     # Again, the right syntax for early binding is not clear
     y = x + 2
def f():
     print x, y

(Similar to some of the ideas about statement local namespaces that were kicking 
around a while back, but in this case specific to function definitions)

Anyway, if others agree that the ability to execute a suite at def exeuction 
time to preinitialise a function's locals without resorting to bytecode hacks is 
worth having, finding a decent syntax is the next trick :)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list