
Nick Coghlan dixit (2011-06-13, 21:57):
def do_and_remember(val, verbose=False): @def mem=collections.Counter() result = do_something(val) mem[val] += 1 if verbose: print('Done {} times for {!r}'.format(_mem[val], val))
The @def ("at def") statement is just a new flavour of the same proposal that has been made many times before: a way to indicate that a simple assignment statement should be executed once at function definition time rather than repeatedly on every call to the function.
If using '@' character, I'd rather prefer: @in(mem=collections.Counter()) def do_and_remember(val, verbose=False): result = do_something(val) mem[val] += 1 if verbose: print('Done {} times for {!r}'.format(_mem[val], val)) @in (or @with, or @within, or @withlocal, or...) could be a language syntax construct, not a real decorator, though using -- already well settled -- decorator-like syntax. Important advantage of this variant is IMHO that then it is obvious for everybody that the binding(s) is (are) being done *early*. Regards. *j