Overriding a global

Terry Reedy tjreedy at udel.edu
Sat Dec 10 19:14:16 EST 2011


On 12/10/2011 3:47 PM, Roy Smith wrote:
>
> What I really want to do is:
>
>     def function(self):

Add a global statement to rebind a global name:
        global logger

>        logger = logger.getChild('function')
>        logger.debug('stuff')
>        logger.debug('other stuff')
>
> which lets me not have to change any lines of code other than inserting
> the one to redefine logger.  Unfortunately, that's not legal Python (it
> leads to "UnboundLocalError: local variable 'logger' referenced before
> assignment").
>
> Any ideas on the best way to implement this?


-- 
Terry Jan Reedy




More information about the Python-list mailing list