local variable referenced before assignment

Stephen Hansen apt.shansen at gmail.invalid
Mon Apr 5 20:55:04 CEST 2010

On 2010-04-05 10:08:51 -0700, John Nagle said:
>     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.

Uhh, what?

>> class A:
...     def handle(self, foo, bar):
...             print "Okay"
>>> a = A()
>>> signal.signal(signal.SIGALRM, a.handle)
>>> Okay

Where after that call to signal.signal, I did kill -ALRM and such in 
another process.

When Python says 'a function', it doesn't mean a -plain- function. A 
method's a function too. Arguably, really, any callable is almost 
always (as in I can't think of anywhere it doesn't) sufficient to be 
Functiony enough to work.


... p.s: change the ".invalid" to ".com" in email address to reply privately.

More information about the Python-list mailing list