473,666 Members | 2,278 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.A pp):
def OnInit(self):
window = KFrame(None, "Testerdete st", (1000,900))
self.SetTopWind ow(window)
window.Show(Tru e)
return True

app = Toepassing(None )
app.MainLoop()

In the file GUIclasses.py I have the statements:

import wx
import wx.lib.mixins.l istctrl as listmix

class KFrame(wx.Frame ):
def __init__(self, parent, title, Size):
...some code...
self.lst = settings.attrLi jst
....some more code......

Now if I run the main.py file I get the error:

File "G:\Programmere n\Codes contactenlijst\ GUIclasses.py", line 40,
in __init_
_
self.lst = settings.attrLi jst
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 1459
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.A pp):
def OnInit(self):
window = KFrame(None, "Testerdete st", (1000,900))
self.SetTopWind ow(window)
window.Show(Tru e)
return True

app = Toepassing(None )
app.MainLoop()

In the file GUIclasses.py I have the statements:

import wx
import wx.lib.mixins.l istctrl as listmix

class KFrame(wx.Frame ):
def __init__(self, parent, title, Size):
...some code...
self.lst = settings.attrLi jst
....some more code......

Now if I run the main.py file I get the error:

File "G:\Programmere n\Codes contactenlijst\ GUIclasses.py", line 40,
in __init_
_
self.lst = settings.attrLi jst
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.A pp):
def OnInit(self):
window = KFrame(None, "Testerdete st", (1000,900))
self.SetTopWind ow(window)
window.Show(Tru e)
return True

app = Toepassing(None )
app.MainLoop()

In the file GUIclasses.py I have the statements:

import wx
import wx.lib.mixins.l istctrl as listmix

class KFrame(wx.Frame ):
def __init__(self, parent, title, Size):
...some code...
self.lst = settings.attrLi jst
....some more code......

Now if I run the main.py file I get the error:

File "G:\Programmere n\Codes contactenlijst\ GUIclasses.py", line 40,
in __init_
_
self.lst = settings.attrLi jst
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

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

Similar topics

2
2612
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): funcname = 'do_' + cmd if hasattr(self, funcname): getattr(self, funcname)()
2
1423
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 if I have a class with same name "BaseClass" in both the file How would I use it if I declare it as given below in my 3rd class
7
1337
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 MyNamespace.Foo
1
1271
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 and other classes used for cryptography shall I build a new Solution named "Utilities"? And how many projects shall I set in it? Let's suppose I need to have two namespaces "Utilities.Log" and "Utilities.Cryptography", in the same assembly......
2
1368
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 and other classes used for cryptography shall I build a new Solution named "Utilities"? And how many projects shall I set in it? Let's suppose I need to have two namespaces "Utilities.Log" and "Utilities.Cryptography", in the same assembly......
0
8440
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8866
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8550
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7381
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6191
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4365
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
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 we have to send another system
2
2006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1769
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.