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

Terry Reedy tjreedy at udel.edu
Mon Jun 13 21:57:31 CEST 2011


On 6/13/2011 10:33 AM, Steven D'Aprano wrote:

> def hook(*args):
> pass
>
> def do_work(args):
> hook("doing spam")
> spam()
> hook("doing ham")
> ham()

Given the expense of function calls, I would write the above as

hook = None

def do(args):
   if hook: hook("doing spam")
   ...

if __name__ == '__main__':
     if '--verbose' in sys.argv:
         wrap = inject(hook=print)

I do not see the point of all this complication. If you are not trying 
to optimize the function (and adding such hooks is obviously not),
         hook = print
works just fine (in 3.x ;-).

-- 
Terry Jan Reedy




More information about the Python-ideas mailing list