local variable referenced before assignment

John Nagle nagle at animats.com
Mon Apr 5 19:08:51 CEST 2010

Alf P. Steinbach wrote:

> Best is however to recognize that you have some state (your variable) 
> and some operations on that state (your callback), and that that is what 
> objects are all about. I.e. wrap your logic in a class. Then 
> 'lastModifiedTime' becomes an instance attribute, and 'handler' becomes 
> a method.
> It doesn't matter that there will only ever be one object (instance) of 
> that class.
> Classes were meant for just this sort of thing, state + operations.

    Yes.  Functions with persistent state are generally a bad idea.

    Unfortunately, the "signal" module requires a callback parameter
which is a plain function.  So you have to send it a function,
closure, or lambda.  Here, it's being sent a closure - "handler"
bound to the state that existed when "signal.signal" was called.

					John Nagle

More information about the Python-list mailing list