对命名空间感到困惑 [英] Confused about namespaces
问题描述
大家好,
开始,对不起,我还在学习编程,
可能我正在制作一些基础知识这里的错误...
我在同一个
目录中有文件settings.py,GUIclasses.py和main.py.在main.py文件中是声明:
导入设置
来自GUIclasses导入的
*
类Toepassing (wx.App):
def OnInit(个体经营):
window = KFrame(无,Testerdetest,(1000,900))
self.SetTopWindow(窗口)
window.Show(True)
返回True
app = Toepassing(无)
app.MainLoop()
在GUIclasses.py文件中我有以下声明:
import wx
导入wx.lib.mixins.listctrl作为listmix
类KFrame(wx.Frame):
def __init __(自我,父母,标题,大小):
...一些代码...
self.lst = settings.attrLijst
.. ..更多代码......
现在,如果我运行main.py文件,我会收到错误:
文件G:\Programmeren \Codes contactenlijst\GUIclasses.py,第40行,
in __init_
_
self。 lst = settings.a ttrLijst
NameError:名称''settings''未定义
为什么会这样?由于来自GUIclasses import *这个KFrame现在处于
最低的范围内。命名空间,因此应该能够使用生活在那里的任何
变量,包括settings。*,no?
提前致谢!
- 基斯
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?
Thanks in advance!
- Kees
推荐答案
2005年11月18日15:04:23 -0800 ,KvS< ke *********** @ gmail.com>写道:
On 18 Nov 2005 15:04:23 -0800, KvS <ke***********@gmail.com> wrote:
大家好,
首先,对不起,我还在学习编程,
可能是我犯了一些根本性的错误这里......
我在同一个目录中有文件settings.py,GUIclasses.py和main.py.在main.py文件中是语句:
导入设置来自GUIclasses import *
类Toepassing(wx.App):
def OnInit (个体经营):
window = KFrame(无,Testerdetest,(1000,900))
self.SetTopWindow(窗口)
window.Show(True)
返回真的
app = Toepassing(无)
app.MainLoop()
在GUIclasses.py文件中,我有以下声明:
> import wx
导入wx.lib.mixins.listctrl作为listmix
类KFrame(wx.Frame):
def __init __(self,parent,title,Size):
...一些代码...
self.lst = settings.attrLijst
....更多代码......
现在如果我运行main.py文件我收到错误:
文件" G:\Programmeren \Codes contactenlijst\GUIclasses.py",第40行,
in __init_ > _
self.lst = settings.attrLijst
NameError:名称''settings''未定义
为什么会这样?由于来自GUIclasses import *这个KFrame现在处于最低的状态。命名空间,因此应该能够使用生活在那里的任何变量,包括settings。*,no?
import不像C / C ++ #include - 导入模块中的代码
在其自己的命名空间中进行评估,而不是在
导入文件的命名空间中进行评估。
所以不,这是预期的行为,也应该通过在GUIclasses.py中导入
设置来解决。
你不要不需要担心多次或多次进口。
提前致谢!
- 基斯
-
http://mail.python.org/mailman/listinfo/ python-list
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
2005年11月18日15:04:23 -0800,KvS< ke ******** ***@gmail.com>写道:
On 18 Nov 2005 15:04:23 -0800, KvS <ke***********@gmail.com> wrote:
大家好,
首先,对不起,我还在学习编程,
可能是我犯了一些根本性的错误这里......
我在同一个目录中有文件settings.py,GUIclasses.py和main.py.在main.py文件中是语句:
导入设置来自GUIclasses import *
类Toepassing(wx.App):
def OnInit (个体经营):
window = KFrame(无,Testerdetest,(1000,900))
self.SetTopWindow(窗口)
window.Show(True)
返回真的
app = Toepassing(无)
app.MainLoop()
在GUIclasses.py文件中,我有以下声明:
> import wx
导入wx.lib.mixins.listctrl作为listmix
类KFrame(wx.Frame):
def __init __(self,parent,title,Size):
...一些代码...
self.lst = settings.attrLijst
....更多代码......
现在如果我运行main.py文件我收到错误:
文件" G:\Programmeren \Codes contactenlijst\GUIclasses.py",第40行,
in __init_ > _
self.lst = settings.attrLijst
NameError:名称''settings''未定义
为什么会这样?由于来自GUIclasses import *这个KFrame现在处于最低的状态。命名空间,因此应该能够使用生活在那里的任何变量,包括settings。*,no?
import不像C / C ++ #include - 导入模块中的代码
在其自己的命名空间中进行评估,而不是在
导入文件的命名空间中进行评估。
所以不,这是预期的行为,也应该通过在GUIclasses.py中导入
设置来解决。
你不要不需要担心多次或多次进口。
提前致谢!
- 基斯
-
http://mail.python.org/mailman/listinfo/ python-list
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
好的,有道理,但似乎并不是自然的对我来说,虽然这是你刚刚指出的明显结果,即模块
在他们自己的命名空间中进行评估,需要注意的事项......在
另一方面,它确实以递归方式工作另一方面
周围既然我没有在main.py中明确导入wx,而只是通过导入wx导入的GUIclasses来间接导入
间接?
谢谢。 :)。
Ok, makes sense but didn''t seem "natural" to me, although it is an
obvious consequence of what you just pointed out, namely that modules
are evaluated in their own namespace, something to keep in mind... On
the other hand it does apparently work recursively "the other way
around" since I didn''t explicitly import wx in main.py but only
indirect via importing GUIclasses in which wx is imported right?
Thanks. :).
这篇关于对命名空间感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!