Overriding a global

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


On 12/10/2011 7:14 PM, Terry Reedy wrote:
> 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

But I see that that is not what you want to do, which is to override the 
global name just within the function while still accessing the global 
name. MRAB's solution does that nicely.

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