callback confusion

D

Donn Ingle

Hi,
I have two modules, the second is imported by the first.
Let's call them one.py and two.py (two being my API)

In the first I register a function into the second.
[in one.py]
def boo(): ...
....
two.register( boo )

two.startLoop()

In two.py it starts a loop (GTK timeout), so processing remains there.
At some point it's time to call the callback.

[within the loop in two.py]
f = getFunc()
f()

That works, it calls boo( ) in one.py -- all fine. The thing I can't figure
out is that it's calling it from the namespace p.o.v of two.py -- For
example:

[in one.py]
kills=0
def boo():
print kills

It will abort with:
print kills
UnboundLocalError: local variable 'kills' referenced before assignment

How can I get the 'self' of a module? If I could tell it to [print
moduleself.kills] or something like that I'd be in business again.
It would be best to have no funny stuff on the user-end of the API; a
simple [print kills] would be best.

\d
 
B

Bruno Desthuilliers

Donn Ingle a écrit :
(snip)
[in one.py]
kills=0
def boo():
print kills

It will abort with:
print kills
UnboundLocalError: local variable 'kills' referenced before assignment

As far as I can tell, you have a bit more code in boo, and somewhere in
that code (after the print statement), you rebind the name 'kills'. In
the absence of a global declaration, this makes this name a local
variable. FWIW, this is a FAQ.
 
D

Donn Ingle

As far as I can tell, you have a bit more code in boo, and somewhere in
that code (after the print statement), you rebind the name 'kills'.
Okay, yes:
def boo()
kills += 1
print kills
the absence of a global declaration, this makes this name a local
variable.
I think I see what you mean:
.... kills += 1
.... print kills
....Traceback (most recent call last):
File "<stdin>", line 1, in <module>
77

I'm amazed that I've spent so much time with Python and something like that
totally stumps me!?
FWIW, this is a FAQ.
If you have a link, that'll help.

\d
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top