1

I can't change a value from a class.

This is my problem :

#!/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()

i want to change the var_a list value everywhere When i click a button ; i am taking an error message :

==========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

i want to change var_a list value by clicking . not only in class , everywhere. i tried using "global" but i cant :( (i took an error message).

How can i solve this problem ?

UnboundLocalError: local variable 'var_a' referenced before assignment

i don't know python well i am beginner. so if u write the code i will be very happy :) thanks a lot !!

3
  • Try moving this line before the class definition : Commented Dec 23, 2012 at 15:28
  • var_a=["30", "31", "32", "33", "34", "35", "36", "37", "38", "39" ] Commented Dec 23, 2012 at 15:28
  • The best answer is not to use globals. They are a bad idea in general, as they make it much harder to follow the code. Commented Dec 23, 2012 at 15:31

1 Answer 1

3

Your method defines a new local variable called var_a that hides the global variable with the same name.

To fix it you can add a global declaration at the beginning of the method:

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================="

However I would advise you not to use globals if it is at all possible. Consider using an instance variable instead.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.