473,508 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Confused about namespaces

KvS
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

Nov 22 '05 #1
22 1432
On 18 Nov 2005 15:04:23 -0800, KvS <ke***********@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

Nov 22 '05 #2
On 18 Nov 2005 15:04:23 -0800, KvS <ke***********@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

Nov 22 '05 #3
KvS
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. :).

Nov 22 '05 #4
KvS
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. :).

Nov 22 '05 #5
On 18 Nov 2005 15:29:43 -0800, KvS <ke***********@gmail.com> wrote:
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?

it worked because all the code that used stuff from the wx namespace
was in GUIclasses.py. If you tried to use create wx classes directly
in your toplevel file it would fail unless you did an "import wx"
first.

Now, as a slight consequence of the specific way you imported, it may
appear to do something different - when GUIClasses.py imported wx, an
entry in the modules namespace was created with the value "wx", and
the value being the wx module. When you do "from GUIClasses import *",
that imports all the symbols in GUIClasses namespace into your local
namespace, including the "wx" entry. This kind of cascading behavior
is one reason that "from foo import *" is frowned up, because the
namespace pollution can cause confusing bugs.
Thanks. :).

--
http://mail.python.org/mailman/listinfo/python-list

Nov 22 '05 #6
On 18 Nov 2005 15:29:43 -0800, KvS <ke***********@gmail.com> wrote:
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?

it worked because all the code that used stuff from the wx namespace
was in GUIclasses.py. If you tried to use create wx classes directly
in your toplevel file it would fail unless you did an "import wx"
first.

Now, as a slight consequence of the specific way you imported, it may
appear to do something different - when GUIClasses.py imported wx, an
entry in the modules namespace was created with the value "wx", and
the value being the wx module. When you do "from GUIClasses import *",
that imports all the symbols in GUIClasses namespace into your local
namespace, including the "wx" entry. This kind of cascading behavior
is one reason that "from foo import *" is frowned up, because the
namespace pollution can cause confusing bugs.
Thanks. :).

--
http://mail.python.org/mailman/listinfo/python-list

Nov 22 '05 #7
KvS
Hmm. But actually I was doing this import from GUIclasses with exactly
this in mind, namely that it would make wx also available at top level.
I (in my naive understanding) see this as "natural" and actually
desirable, how could this cause confusing bugs? Do you mean multiple
"from ... import *"'s 'on top of each other' would cause foo1.foo2.attr
and foo1.attr both would become just attr and therefore ambiguous at
top level?

If you import foo in two different modules, does the interpreter then
create one instance of foo and create a reference in both modules to
the same foo rather than creating two different instances of foo?

Nov 22 '05 #8
KvS
Hmm. But actually I was doing this import from GUIclasses with exactly
this in mind, namely that it would make wx also available at top level.
I (in my naive understanding) see this as "natural" and actually
desirable, how could this cause confusing bugs? Do you mean multiple
"from ... import *"'s 'on top of each other' would cause foo1.foo2.attr
and foo1.attr both would become just attr and therefore ambiguous at
top level?

If you import foo in two different modules, does the interpreter then
create one instance of foo and create a reference in both modules to
the same foo rather than creating two different instances of foo?

Nov 22 '05 #9
On 18 Nov 2005 16:09:44 -0800, KvS <ke***********@gmail.com> wrote:
Hmm. But actually I was doing this import from GUIclasses with exactly
this in mind, namely that it would make wx also available at top level.
There's no reason not to just "import wx" if you want that.
I (in my naive understanding) see this as "natural" and actually
desirable, how could this cause confusing bugs? Do you mean multiple
"from ... import *"'s 'on top of each other' would cause foo1.foo2.attr
and foo1.attr both would become just attr and therefore ambiguous at
top level?
the second import will overwrite the first, making the first inaccessible

If you import foo in two different modules, does the interpreter then
create one instance of foo and create a reference in both modules to
the same foo rather than creating two different instances of foo?

No. It creates the foos within each module, but which foo you have
access to in the importing module is determined by the order of
import.
--
http://mail.python.org/mailman/listinfo/python-list

Nov 22 '05 #10
On 18 Nov 2005 16:09:44 -0800, KvS <ke***********@gmail.com> wrote:
Hmm. But actually I was doing this import from GUIclasses with exactly
this in mind, namely that it would make wx also available at top level.
There's no reason not to just "import wx" if you want that.
I (in my naive understanding) see this as "natural" and actually
desirable, how could this cause confusing bugs? Do you mean multiple
"from ... import *"'s 'on top of each other' would cause foo1.foo2.attr
and foo1.attr both would become just attr and therefore ambiguous at
top level?
the second import will overwrite the first, making the first inaccessible

If you import foo in two different modules, does the interpreter then
create one instance of foo and create a reference in both modules to
the same foo rather than creating two different instances of foo?

No. It creates the foos within each module, but which foo you have
access to in the importing module is determined by the order of
import.
--
http://mail.python.org/mailman/listinfo/python-list

Nov 22 '05 #11
KvS
> There's no reason not to just "import wx" if you want that.

Yes, that's clear. But if you would make some huge application that has
a large number of nested modules, each importing the former one, then
avoiding the use of "from ... import *" would mean that you have to use
long references like foo1.foo2.... to get to the lowest modules plus
that you'd have to check each module for imports outside this tree.

If you would use "from ... import *" (except at top level) you have to
be aware of overriding, but you can also use this to your advantage and
any foo1.attr reference would just work right, without further ado...
Or would in such a case a class hierarchy be the thing to use?
No. It creates the foos within each module, but which foo you have
access to in the importing module is determined by the order of
import.


Am I understanding correctly that if you have a module foo importing wx
and a module main importing both foo and wx there are actually two
instances of wx created, one referenced to by (at top level) foo.wx.*
and one wx.*? If this is indeed the case it isn't too good for the
performance doing this importing of wx multiple times right?

- Kees

Nov 22 '05 #12
KvS
> There's no reason not to just "import wx" if you want that.

Yes, that's clear. But if you would make some huge application that has
a large number of nested modules, each importing the former one, then
avoiding the use of "from ... import *" would mean that you have to use
long references like foo1.foo2.... to get to the lowest modules plus
that you'd have to check each module for imports outside this tree.

If you would use "from ... import *" (except at top level) you have to
be aware of overriding, but you can also use this to your advantage and
any foo1.attr reference would just work right, without further ado...
Or would in such a case a class hierarchy be the thing to use?
No. It creates the foos within each module, but which foo you have
access to in the importing module is determined by the order of
import.


Am I understanding correctly that if you have a module foo importing wx
and a module main importing both foo and wx there are actually two
instances of wx created, one referenced to by (at top level) foo.wx.*
and one wx.*? If this is indeed the case it isn't too good for the
performance doing this importing of wx multiple times right?

- Kees

Nov 22 '05 #13
> Am I understanding correctly that if you have a module foo importing wx
and a module main importing both foo and wx there are actually two
instances of wx created, one referenced to by (at top level) foo.wx.*
and one wx.*? If this is indeed the case it isn't too good for the
performance doing this importing of wx multiple times right?


No, they aren't two instances. The first import will evaluate the
wx-module (there could be code in there executed upon importing it). All
subsequent imports of that module will only bind the already imported
module to the name.

So - the way to have one module be available in several other modules is
to import them in each of them - causing actually close to none
overhead, as the import statement is evaluated only once per file
(unless you put it in a loop or something, but this still only would
return the same reference.)

Regards,

Diez
Nov 22 '05 #14
> Am I understanding correctly that if you have a module foo importing wx
and a module main importing both foo and wx there are actually two
instances of wx created, one referenced to by (at top level) foo.wx.*
and one wx.*? If this is indeed the case it isn't too good for the
performance doing this importing of wx multiple times right?


No, they aren't two instances. The first import will evaluate the
wx-module (there could be code in there executed upon importing it). All
subsequent imports of that module will only bind the already imported
module to the name.

So - the way to have one module be available in several other modules is
to import them in each of them - causing actually close to none
overhead, as the import statement is evaluated only once per file
(unless you put it in a loop or something, but this still only would
return the same reference.)

Regards,

Diez
Nov 22 '05 #15
KvS <ke***********@gmail.com> wrote:
There's no reason not to just "import wx" if you want that.
Yes, that's clear. But if you would make some huge application that has
a large number of nested modules, each importing the former one, then
avoiding the use of "from ... import *" would mean that you have to use
long references like foo1.foo2.... to get to the lowest modules plus


Not at all -- you may perfectly well "import a.b.c.d as e" and use e as
the reference throughout the importing module.

But the point is, modules should be MODULAR (duh) -- if I 'import x' I
should NOT rely on what x itself imports (and so on transitively), just
on what x ``exports'', so to speak, at its TOP level. The Law of
Demeter in its simplified form "if you have more than one dot in your
reference you're doing things wrong" applies. In fact, the many-dots
form of module naming is intended for nested PACKAGES, *NOT* for
"modules which import each other".
that you'd have to check each module for imports outside this tree.


Why would you have to check any module for such "outside imports"? I
don't see the need for any checks.
If you just simply forget the very _existence_ of "from X import *",
your Python code can only grow better as a result. I earnestly hope it
disappears come Python 3.0 time...
Alex
Nov 22 '05 #16
KvS <ke***********@gmail.com> wrote:
There's no reason not to just "import wx" if you want that.
Yes, that's clear. But if you would make some huge application that has
a large number of nested modules, each importing the former one, then
avoiding the use of "from ... import *" would mean that you have to use
long references like foo1.foo2.... to get to the lowest modules plus


Not at all -- you may perfectly well "import a.b.c.d as e" and use e as
the reference throughout the importing module.

But the point is, modules should be MODULAR (duh) -- if I 'import x' I
should NOT rely on what x itself imports (and so on transitively), just
on what x ``exports'', so to speak, at its TOP level. The Law of
Demeter in its simplified form "if you have more than one dot in your
reference you're doing things wrong" applies. In fact, the many-dots
form of module naming is intended for nested PACKAGES, *NOT* for
"modules which import each other".
that you'd have to check each module for imports outside this tree.


Why would you have to check any module for such "outside imports"? I
don't see the need for any checks.
If you just simply forget the very _existence_ of "from X import *",
your Python code can only grow better as a result. I earnestly hope it
disappears come Python 3.0 time...
Alex
Nov 22 '05 #17
KvS a écrit :
Ok, makes sense but didn't seem "natural" to me,
It will seem more natural if you understand that modules should be
modulars (ie: low coupling, high cohesion). A module should *never*
bother about no rely upon other modules being imported by the module it
imports itself. Err, not quite clear... Let's rephrase it : a module
should only used symbols it defines itself or that it *explicitely*
imports from other modules.
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?


You've already got technical answers to this - please carefully (re)read
Alex Martelli's post.

Now about coding style: this is *very* Bad Style(tm). Your main module
imports should look like this:

import wx
# import settings -> we don't use the settings module here
from GUIclasses import KFrame

(...)

and the GUIclasses module's import :

import wx
import wx.lib.mixins.listctrl as listmix
import settings -> *here* we use the settings module

(...)

Python's philosophy is to favour readability.

Talking about imports, take time to open your Python interactive shell
and type "import this" !-)

Nov 22 '05 #18
KvS a écrit :
Ok, makes sense but didn't seem "natural" to me,
It will seem more natural if you understand that modules should be
modulars (ie: low coupling, high cohesion). A module should *never*
bother about no rely upon other modules being imported by the module it
imports itself. Err, not quite clear... Let's rephrase it : a module
should only used symbols it defines itself or that it *explicitely*
imports from other modules.
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?


You've already got technical answers to this - please carefully (re)read
Alex Martelli's post.

Now about coding style: this is *very* Bad Style(tm). Your main module
imports should look like this:

import wx
# import settings -> we don't use the settings module here
from GUIclasses import KFrame

(...)

and the GUIclasses module's import :

import wx
import wx.lib.mixins.listctrl as listmix
import settings -> *here* we use the settings module

(...)

Python's philosophy is to favour readability.

Talking about imports, take time to open your Python interactive shell
and type "import this" !-)

Nov 22 '05 #19
KvS
Thanks a lot for all the answers. After rereading everything said here
today it's become more clear to me what you guys are telling me and
I'll actively try to forget about "from ... import *" ;).

Nov 22 '05 #20
KvS
Thanks a lot for all the answers. After rereading everything said here
today it's become more clear to me what you guys are telling me and
I'll actively try to forget about "from ... import *" ;).

Nov 22 '05 #21
KvS <ke***********@gmail.com> wrote:
Thanks a lot for all the answers. After rereading everything said here
today it's become more clear to me what you guys are telling me and
I'll actively try to forget about "from ... import *" ;).


I commend you for your decision. It's a construct that I sometimes find
quite handy in an experimentation session in the interactive
interpreter, but just has no really good place in 'true' code;-0.
Alex

Nov 22 '05 #22
KvS <ke***********@gmail.com> wrote:
Thanks a lot for all the answers. After rereading everything said here
today it's become more clear to me what you guys are telling me and
I'll actively try to forget about "from ... import *" ;).


I commend you for your decision. It's a construct that I sometimes find
quite handy in an experimentation session in the interactive
interpreter, but just has no really good place in 'true' code;-0.
Alex

Nov 22 '05 #23

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2604
by: Brian Roberts | last post by:
I'm confused about the use of hasattr/getattr, or possibly namespaces. I know how to do this: class UnderstandThis(object): def do_foo(self): pass def do_bar(self): pass def doit(self, cmd):...
2
1417
by: Iyer, Prasad C | last post by:
Actually I am bit confused between the modules and .py file How do I differentiate between the 2. For example I have a file import1.py, import2.py file Which has few functions and classes And...
7
1333
by: Dylan Parry | last post by:
Hi, I've started to get a bit confused by namespaces and hierarchical organisation in general. What I want to do is something like this: Namespace MyNamespace | |- Class Foo | |- Namespace...
1
1265
by: Giulio Petrucci | last post by:
Hi everybody, I'm getting confused about "which-name-give-to-what" developing my applications using Visual Studio. If I have to develop a "Utilities" library containing some classes for logging...
2
1361
by: Giulio Petrucci | last post by:
Hi everybody, I'm getting confused about "which-name-give-to-what" developing my applications using Visual Studio. If I have to develop a "Utilities" library containing some classes for logging...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.