question about scope

John Salerno johnjsal at NOSPAMgmail.com
Sun Oct 1 05:00:43 CEST 2006


I have the following code:



class DataAccessFrame(wx.Frame):

     menu_items = [('File', 'New Database', 'New Record', 'Open 
Database...',
                    'Open Record...', 'Save Record', 'Save All Records',
                    'Close Record', 'Close Database'),
                   ('Edit', 'Undo', 'Redo', 'Cut', 'Copy', 'Paste'),
                   ('Help',)]

     def __init__(self):
         wx.Frame.__init__(self, None, title='Database Access Panel')
         panel = wx.Panel(self)
         self.create_menubar()
#       notebook = wx.Notebook(panel)

#       sizer = wx.BoxSizer()
#       sizer.Add(notebook, 1, wx.EXPAND)
#       panel.SetSizer(sizer)

     def create_menubar(self):
         menubar = wx.MenuBar()
         for item in self.menu_items:
             menu = wx.Menu()
             menubar.Append(menu, item[0])
             for subitem in item[1:]:
                 menu.Append(-1, subitem)
         self.SetMenuBar(menubar)

In the create_menubar method, I got an error about the global name 
"menu_items" not being defined, and this was fixed by putting "self." in 
front of the variable name.

But why is this necessary? Doesn't a method look in its enclosing class, 
or is that not one of the levels of scope?



More information about the Python-list mailing list