I don’t want to drag everyone back to the basics but I’m having a heck of a time trying to figure out a “Globals” issue
global name 'renderQueuePaths' is not defined
Don’t worry I’ve visited the oracle(google) and been to stack overflow. I understand the concept of local and global variables in python but my code still isn’t working and it’s too long to post.
Basically I have a list widget and I add things to it and remove things from it. I also have a list variable that is acting as the data on the back end. I have placed it outside my def and and declared it as a global inside my def but I still get the error.
Now if I assign it like so :
renderQueuePaths = []
I don’t get the error but that defeats the purpose because I want to use the info that is assigned to the list when adding it.:sigh: does this make sense?
or is there a completely better way to do this?
Can you give a bit more context? I’m guessing you’re doing something like this:
global_list = [1,2,3,4,5]
def print_global_list():
for g in global_list:
print(g)
def append_to_global_list():
global global_list
global_list.append(6)
print_global_list()
1
2
3
4
5
append_to_global_list()
print_global_list()
1
2
3
4
5
6
Seems pretty straightforward. You could always try making a class and making your list a class variable too.
As Seth demonstrated in his code the global keyword in Python must be used both at declaration time and whenever you want to access that variable. I’m guessing you used global at declaration but are not using it when you are trying to get your list from within your method.
@Seth: Your example is pretty much what i’m doing, except I have the globals declared inside the class I have. Can they just be sitting outside that in la-la land? I’m used to more structured languages I guess. it looks more like:
class MyClass():
global_list = ['1', '2', '3']
def addToList():
globalList.append('10')
all the functions I use are within the same class. I will try sticking outside the class tonight and I still have issues I will post the code. Thanks for your Seth.
You too Jeff.
Matt, what you’ve done there is create an attribute called “global_list” on the class object. Remember, classes are objects just like instances of those classes are.
This is totally valid for some purposes, but probably not for what you’re after here. Changing class attributes from inside an instance of that class will not automatically apply to other instances of that class. They have their own copy of that class attribute, created when they were instantiated.
If you move “global_list” outside the class and make it a global variable at the module level as Seth illustrated, it should do what you expect.
Matt, I just saw your note about all functions being in that same class. If that’s the case, you don’t need a global at all. Just turn it into an instance attribute via init. Like this:
class MyClass():
def __init__( self ):
self.mainList = [ '1', '2', '3' ]
def addToList( self ):
self.mainList.append( '10' )
Then every method in MyClass can reach/change that list via “self.mainList”
What Adam said. Class variables are always a good way to go.
You know I was thinking of trying that but none of the example files I have been looking through used that…then again none of those are trying to do what I’m doing. Thanks a bunch for the help. I will be posting the program with a link when I’m done. hopefully by the end of the week.
The class variables thing worked the first time, i’ll stick to that until I learn the whole Model view programming thing. Thanks again!