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

Arnaud Delobelle arnodel at gmail.com
Mon Jun 13 14:22:07 CEST 2011


On 13 June 2011 12:57, Nick Coghlan <ncoghlan at gmail.com> wrote:

> As yet another shade for this particular bikeshed, this one just occurred to me:
>
>    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))

Or to link this to PEP 3150:

    given:
          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))

(Or the other way around)

-- 
Arnaud



More information about the Python-ideas mailing list