Re: UnboundLocalError problems
- From: Terry Reedy <tjreedy@xxxxxxxx>
- Date: Sun, 29 Jun 2008 18:53:26 -0400
Mr SZ wrote:
Hi,
I am writing a small script that changes my pidgin status to away when I lock my screen.I'm using the DBUS API for pidgin and gnome-screensaver.Here's the code:
#!/usr/bin/env python
import dbus, gobject
from dbus.mainloop.glib import DBusGMainLoop
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
obj = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")
STATUS_AVAILABLE = 2
STATUS_AWAY = 5
ORIG_TYPE = 0
ORIG_MSG = ""
def my_func2(IsEnabled):
you need
global ORIG_TYPE,ORIG_MSG
if IsEnabled:
ORIG_TYPE,ORIG_MSG = get_current()
because this otherwise marks them as local.
[snip]
When I run it,it errors out at :
set_status(ORIG_TYPE,ORIG_MSG)
giving the error "UnboundLocalError: local variable 'ORIG_TYPE' referenced before assignment" .This happens
In the future, copy and paste the full traceback, which often has additional useful information -- like the line # of the call -- which is not so obvious to someone who has not read the code.
Aren't ORIG_TYPE and ORIG_MSG global variables? Where am I going wrong?Everything works fine if I set the arguments manually to the set_status function.
*Any* assignment to a name, including with def, class, import, and augmented assignment, even if the statement is unreachable and cannot be executed, marks the name as local -- unless you specify it as global (or nonlocal in 3.0).
tjr
.
- Prev by Date: Re: Function to import module to namespace
- Next by Date: Re: Function to import module to namespace
- Previous by thread: list extension ?
- Next by thread: How to invoke the Python idle
- Index(es):