[Python-ideas] 'Injecting' objects as function-local constants

Jan Kaliszewski zuo at chopin.edu.pl
Tue Jun 14 03:12:14 CEST 2011


Jan Kaliszewski dixit (2011-06-14, 00:30):

>     @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

On second thought: no. I mean: no -- for a separate syntax construct
with limited usage possibilities (see: cases mentioned by Steven);
yes -- for language improvements that would make possible one of the
solutions:

1. A real decorator:
  a) quasi-argument-locals-based
     (names could be used to read injected value and later could be
      rebound, like arguments);
or
  b) another-level-closure-based
     (names could not be used to read injected values if rebound later:
      it's *either* a free variable *or* a local variable).

or

2. `after-** hidden pseudo-arguments' (see previous posts...).

Now I don't know which of them I'd prefer...

And probably any of them would need some core-language modifications...
(at least the '2' and '1a' variants) 

Regards.
*j




More information about the Python-ideas mailing list