Your problem is that Python is not working the way you think.
Therefore, I will try to explain what is happening in your code line by line.
mouse = "a"
Assigns the string "a" to the name of the mouse .
background = "b"
Names the string "b" background .
list_ab = [mouse, background]
Assigns two objects referenced by the names mouse and background to the list_ab list.
As we already know, these are the constants "a" and "b". Therefore, you can simply write:
list_ab = ["a", "b"]
Now loop
for item in list_ab:
Gives each item in the list the name item .
For the first iteration of the loop, this means item = "a" .
Line
global item
does not make sense because it is trying to tell Python that the name item is global, while such a global variable is not declared.
And for the most confusing behavior on the line
item = "modified"
you should just understand that although it assigns the string “changed” to the name item , the lines “a” and “b” are all the same and are still tied to list_ab (and the names of mouse and background that you did not touch either) .
The name item lives only in the area where it was declared, when the for loop ends, it is destroyed. It also reassigns each iteration to the next element from list_ab .
Summarizing:
If you want to assign a “modified” string to the items in the list, do this directly:
list_ab[0] = "modified" list_ab[1] = "modified"
If you need to change a variable declared in the global scope, do the following:
mouse = "a" def func(): global mouse
An example based on the first revision of a question:
Instead
background = g_base("bg.jpg")
You can do:
imgs_to_load = [g_base("bg.jpg"), g_base("mouse.png",alpha=1)] # list with a g_base class instances def Image_loader(img): # ..... code to load the image and convert it into pygame surface... return img_load def main (): imgs_in_pygame_format = [] # create an empty list for image in imgs_to_load: loaded_image = Image_loader(image) imgs_in_pygame_format.append(loaded_image) # add to the list for image in imgs_in_pygame_format: print image # every image in the list is a pygame surface # or print image[0] print image[1] main()
Or, if you want to refer to images by name, you can put them in a dictionary:
imgs_to_load = {} imgs_to_load["bg"] = g_base("bg.jpg") imgs_to_load["mouse"] = g_base("mouse.png",alpha=1) imgs_in_pygame_format = {} # create a global dictionary for loaded images def main (): global imgs_in_pygame_format # import that global name into the local scope for write access for name, data in imgs_to_load.items(): imgs_in_pygame_format[name] = Image_loader(data) # add to the dictionary for image in imgs_in_pygame_format: print image # every image in the dictionary is a pygame surface # or print imgs_in_pygame_format["bg"] print imgs_in_pygame_format["mouse"] main()
But most importantly, global variables are a bad idea . A better solution would be to use a class:
def Image_loader(img): # ..... code to load the image and convert it into pygame surface... return img_load class Image: def __init__(self, image): self.data = Image_loader(image) def main (): bg = Image(g_base("bg.jpg")) mouse = Image(g_base("mouse.png",alpha=1)) print bg.data print mouse.data main()
See the Python Tutorial for more examples.
Hope this helps, and welcome to StackOverflow!