通过类更改变量值(python)

通过类更改变量值(python),python,class,variables,global-variables,Python,Class,Variables,Global Variables,我无法更改类中的值 这是我的问题: #!/usr/bin/env python import pygtk pygtk.require20() import gtk class MyApp(object): def __init__(self): self.pencere = gtk.Window(gtk.WINDOW_TOPLEVEL) self.pencere.connect("delete_event", gtk.main_quit)

我无法更改类中的值

这是我的问题:

#!/usr/bin/env python

import pygtk
pygtk.require20()
import gtk

class MyApp(object):
    def __init__(self):
        self.pencere = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.pencere.connect("delete_event", gtk.main_quit)

        self.b_Up = gtk.Button("UP")
        self.b_Down = gtk.Button("DOWN")
        self.b_Up.connect("pressed", self.ToUp)
        self.b_Down.connect("pressed", self.ToDown)

        self.tablo = gtk.Table(2, 1, False)
        self.tablo.attach(self.b_Up , 0, 1, 0,1)
        self.tablo.attach(self.b_Down, 1, 2, 0, 1 )

        self.pencere.add(self.tablo)

        self.pencere.show_all()
        return

    def ToUp(self, penar  ):
        print "==========ClassUp======= \n" , var_a
        print "@ToUP : " , var_a
        ChangeUp()
        print var_a , "\n=========ClassEndUp========"

    def ToDown(self, penar):
        print "==========ClassDown======= \n", var_a
        print "@ToDown : ", var_a
        ChangeDown()
        print var_a , "\n========ClassEndDown========="

    def main(self):
        gtk.main()

def ChangeUp():
    print "Changing Up : ", var_a
    var_a=["90", "91", "92", "93", "94", "95", "96", "97", "98", "99" ]
    print var_a , "\n================="


def ChangeDown():
    print "Changing Down : " , var_a
    var_a=["0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ]
    print var_a , "\n================="


var_a=["30", "31", "32", "33", "34", "35", "36", "37", "38", "39" ]

app = MyApp()
app.main()
我想在任何地方更改变量a列表值 当我点击一个按钮;我正在接收一条错误消息:

==========ClassDown======= 
['30', '31', '32', '33', '34', '35', '36', '37', '38', '39']
@ToDown :  ['30', '31', '32', '33', '34', '35', '36', '37', '38', '39']
Changing Down : 
Traceback (most recent call last):
  File "aa.py", line 35, in ToDown
    ChangeDown()
  File "aa.py", line 48, in ChangeDown
    print "Changing Down : " , var_a
UnboundLocalError: local variable 'var_a' referenced before assignment
我想通过单击更改列表值。不仅在课堂上,到处都是。我尝试使用“全局”,但我不能:((我收到了一条错误消息)

我怎样才能解决这个问题

UnboundLocalError: local variable 'var_a' referenced before assignment
我不太懂python,我是初学者。所以如果你写代码,我会非常高兴:)
非常感谢

您的方法定义了一个名为
var\u a
的新局部变量,它隐藏了同名的全局变量

要解决此问题,可以在方法开头添加全局声明:

def ChangeDown():
    global var_a
    print "Changing Down : " , var_a
    var_a=["0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ]
    print var_a , "\n================="

但是,如果可能的话,我建议您不要使用globals。考虑使用一个实例变量代替.< /p>尝试在类定义之前移动这行:VARYA= =“30”、“31”、“32”、“33”、“34”、“35”、“36”、“37”、“38”、“39”,最好的答案是不要使用全局变量。一般来说,它们是一个坏主意,因为它们使遵循代码变得更加困难。