Confused about namespaces

Chris Mellon arkanes at gmail.com
Sat Nov 19 00:15:00 CET 2005


On 18 Nov 2005 15:04:23 -0800, KvS <keesvanschaik at gmail.com> wrote:
> Hi all,
>
> to start with, excuse me, I'm still learning programming alltogether,
> probably I'm making some fundamental mistake here...
>
> I have the files settings.py, GUIclasses.py and main.py in the same
> directory. In the file main.py are the statements:
>
> import settings
> from GUIclasses import *
>
> class Toepassing(wx.App):
>         def OnInit(self):
>                 window = KFrame(None, "Testerdetest", (1000,900))
>                 self.SetTopWindow(window)
>                 window.Show(True)
>                 return True
>
> app = Toepassing(None)
> app.MainLoop()
>
> In the file GUIclasses.py I have the statements:
>
> import wx
> import wx.lib.mixins.listctrl  as  listmix
>
> class KFrame(wx.Frame):
>         def __init__(self, parent, title, Size):
>                 ...some code...
>                 self.lst = settings.attrLijst
>                 ....some more code......
>
> Now if I run the main.py file I get the error:
>
>   File "G:\Programmeren\Codes contactenlijst\GUIclasses.py", line 40,
> in __init_
> _
>     self.lst = settings.attrLijst
> NameError: name 'settings' is not defined
>
> Why is this? Since "from GUIclasses import *" this KFrame is now at
> "the lowest" namespace and should therefore be able to make use of any
> variables living there, including "settings.*", no?
>

import is not like a C/C++ #include - the code in the imported module
is evaluated in it's own namespace, not in the namespace of the
importing file.

So no, this is expected behavior, and should be solved by importing
settings in GUIclasses.py as well.

You don't need to worry about multiple or redundent imports.


> Thanks in advance!
>
> - Kees
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list