I'm experiencing a (for me) very weird problem in Python.
I have a class called Menu: (snippet)
class Menu:
"""Shows a menu with the defined items"""
menu_items = {}
characters = map(chr, range(97, 123))
def __init__(self, menu_items):
self.init_menu(menu_items)
def init_menu(self, menu_items):
i = 0
for item in menu_items:
self.menu_items[self.characters[i]] = item
i += 1
When I instantiate the class, I pass in a list of dictionaries. The dictionaries are created with this function:
def menu_item(description, action=None):
if action == None:
action = lambda : None
return {"description": description, "action": action}
And then the lists are created like this:
t = [menu_item("abcd")]
m3 = menu.Menu(t)
a = [ menu_item("Test")]
m2 = menu.Menu(a)
b = [ menu_item("Update", m2.getAction),
menu_item("Add"),
menu_item("Delete")]
m = menu.Menu(b)
When I run my program, I everytime get the same menu items. I've run the program with PDB and found out as soon as another instance of a class is created, the menu_items of all previous classes are set to latest list. It seems as if the menu_items member is static member.
What am I overseeing here?