UnboundLocalError: local variable 'UI_curses' referenced before assignment

Jose Manuel Vivo jmvivoa_c en yahoo.es
Jue Sep 8 11:07:08 CEST 2005


Hola Fernando...

En Python, dentro de los metodo de una clase, para referirse a la instancia de 
la propia clase debes de usar el primer parameto que recibes en el metodo 
(por norma lo llamamos 'self') asi que en
> class UI_curses:
> 	def __init__(self):
> 		UI_curses.mainwin = curses.initscr()
                self.mainwin = curses.initscr()
> 		UI_curses.maxy, UI_curses.maxx = UI_curses.mainwin.getmaxyx()
                self.maxy, self.maxx = self.mainwin.getmaxyx()
> 		UI_curses.y = 0 # actual highlighted line
                self.y = 0
... y asi sucesivamente.

Un saludo.
Chema.



El Miércoles, 7 de Septiembre de 2005 12:45, Fernando Canizo escribió:
> Al correr el script de más abajo me encuentro con el error del tema:
>
> Traceback (most recent call last):
> File "prueba.py", line 124, in ?
> ui.highlight_previous(list)
> File "prueba.py", line 101, in highlight_previous
> UI_curses.listwin.addstr(UI_curses.y, 0, list[UI_curses.y],
> curses.A_NORMAL) UnboundLocalError: local variable 'UI_curses' referenced
> before assignment Exception exceptions.AttributeError: "'NoneType' object
> has no attribute 'mainwin'" in <bound method UI_curses.__del__ of
> <__main__.UI_curses instance at 0xb7c49e6c>> ignored
>
> Tengo luego la exception esa, pero por ahora me interesa el unbound.
>
> Llevo un rato buscando en la web de que se puede tratar mi error, pero
> todas las referencias que encuentro parecen referirse a problemas de
> variables no declaradas, sin embargo en mi script estoy intentando
> acceder a atributos de la clase, por lo tanto no entiendo el error.
>
> Si alguien tiene una pista de por qué se produce el unbound, le
> agradezco que me ilumine. Ya probé cambiando el nombre de la clase y
> todas las referencias a algo que no pudiera colisionar con algún
> posible nombre, sustituí todas las referencias a 'UI_curses' por
> 'COSA' y no viene por ahí la mano, el error es el mismo.
>
> Las funciones que están explotando son las highlight_*
>
> El código que lo genera:
>
> #! /usr/bin/env python
> # -*- coding: utf8 -*-
>
> import curses
>
> class UI_curses:
> 	def __init__(self):
> 		UI_curses.mainwin = curses.initscr()
> 		UI_curses.maxy, UI_curses.maxx = UI_curses.mainwin.getmaxyx()
> 		UI_curses.y = 0 # actual highlighted line
>
> 		UI_curses.helpwin = curses.newwin(1, UI_curses.maxx, 0, 0)
> 		UI_curses.listwin = curses.newwin(UI_curses.maxy - 2, UI_curses.maxx / 2,
> 1, 0) UI_curses.descwin = curses.newwin(UI_curses.maxy - 2, UI_curses.maxx
> / 2, 1, UI_curses.maxx / 2 + 1) UI_curses.statwin = curses.newwin(1,
> UI_curses.maxx, UI_curses.maxy - 1, 0)
>
> 		UI_curses.listwin.keypad(1) # use keypad
> 		UI_curses.descwin.keypad(1)
>
> 		curses.noecho() # echo would be controlled by program
> 		curses.cbreak() # accept input inmediatelly
> 		if curses.has_colors():
> 			curses.start_color()
> 			# for stat and help wins
> 			curses.init_pair(1, curses.COLOR_YELLOW, curses.COLOR_BLUE)
> 			curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLUE)
>
> 	def __del__(self):
> 		UI_curses.mainwin.keypad(0)
> 		curses.nocbreak()
> 		curses.echo()
> 		curses.endwin()
>
> 	def getch(self):
> 		return UI_curses.listwin.getch()
>
> 	def do_helpwin(self, help_string):
> 	# fix: not checking if we have colors, should do
> 		UI_curses.helpwin.clear()
> 		curses.curs_set(0) # don't show cursor
> 		for c in range(help_string.__len__()):
> 			if c > UI_curses.maxx:
> 				stat_win("help_win: string length higher than maxx, some chars loosed")
> 				return
> 			if help_string[c].isupper():
> 				UI_curses.helpwin.addch(ord(help_string[c]), curses.color_pair(2))
> 			else:
> 				UI_curses.helpwin.addch(ord(help_string[c]), curses.color_pair(1))
> 		for c in range(help_string.__len__(), UI_curses.maxx - 1):
> 			UI_curses.helpwin.addch(ord(' '), curses.color_pair(1))
> 		UI_curses.helpwin.redrawwin()
> 		UI_curses.helpwin.refresh()
>
> 	def do_listwin(self, list): # print_tasks|categories
> 		UI_curses.listwin.clear()
> 		for y in range(list.__len__()):
> 			if y == UI_curses.y:
> 				UI_curses.listwin.addstr(y, 0, list[y], curses.A_REVERSE)
> 			else:
> 				UI_curses.listwin.addstr(y, 0, list[y], curses.A_NORMAL)
> 		UI_curses.listwin.redrawwin()
> 		UI_curses.listwin.refresh()
> 		return
>
> 	def do_descwin(self, list = None):
> 		UI_curses.descwin.clear()
> 		if list != None:
> 			for y in range(list.__len__()):
> 				UI_curses.descwin.addstr(y, 0 , list[y], curses.A_NORMAL)
> 		UI_curses.descwin.redrawwin()
> 		UI_curses.descwin.refresh()
>
> 	def do_statwin(self, stat_string):
> 	# fix: not checking if we have colors, should do
> 		UI_curses.statwin.clear()
> 		curses.curs_set(0) # don't show cursor
> 		for c in range(stat_string.__len__()):
> 			if c > UI_curses.maxx:
> 				stat_win("stat_win: string length higher than maxx, some chars loosed")
> 				return
> 			UI_curses.statwin.addch(ord(stat_string[c]), curses.color_pair(1))
> 		for c in range(stat_string.__len__(), UI_curses.maxx - 1):
> 			UI_curses.statwin.addch(ord(' '), curses.color_pair(1))
> 		UI_curses.statwin.redrawwin()
> 		UI_curses.statwin.refresh()
>
> 	def do_inputwin(self):
> 		"""Window to obtain user input."""
> 		inputwin = curses.newwin(1, UI_curses.maxx, 1, 0)
> 		inputwin.refresh()
> 		curses.echo(1)
> 		input = inputwin.getstr(0,0,38)
> 		curses.echo(0)
> 		return input
>
> 	def highlight_previous(self, list):
> 	# fix: both highligts will explode when list.__len__ > listwin.maxy
> 		UI_curses.listwin.addstr(UI_curses.y, 0, list[UI_curses.y],
> curses.A_NORMAL) if UI_curses.y == 0:
> 			y = list.__len__()
> 			UI_curses = y - 1
> 		else:
> 			UI_curses.y -= 1
> 		UI_curses.listwin.addstr(UI_curses.y, 0, list[UI_curses.y],
> curses.A_REVERSE) UI_curses.listwin.refresh()
>
> 	def highlight_next(self, list):
> 		UI_curses.listwin.added(UI_curses.y, 0, list[UI_curses.y],
> curses.A_NORMAL) y = list.__len__()
> 		if UI_curses.y == y - 1:
> 			UI_curses.y = 0
> 		else:
> 			UI_curses.y += 1
> 		UI_curses.listwin.addstr(UI_curses.y, 0, list[UI_curses.y],
> curses.A_REVERSE) UI_curses.listwin.refresh()
>
> # main de prueba
> ui = UI_curses()
> list = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
> ui.do_listwin(list)
> ui.highlight_previous(list)

		
______________________________________________ 
Renovamos el Correo Yahoo! 
Nuevos servicios, más seguridad 
http://correo.yahoo.es




Más información sobre la lista de distribución Python-es