473,388 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Namespaces, classes, and using standard modules

I have several simple classes which need to us "standard" modules like os and sys

A class might look like:

# --------- MyClass.py -----------------------
class MyClass:
def __init__ (self):
self.data = ''
self.data2 = 'mydata'

def show (self):
print "Data is " + self.data
print "Other data is " + self.data2
print "Directory is " + os.getcwd()
# --------------------------------------------
If I do:
import os
from MyClass import MyClass
x = MyClass()
x.show()


it fails with

NameError: global name 'os' is not defined

However, if I put the 'import os' statement in MyClass.py, but OUTSIDE the class definition, it works fine. I can also
(obviously) put the import inside the class definition, then use 'self.os.getcwd()' but that seems to me to be
needlessly complicated.

I would have expected that importing os from the interactive prompt would have worked, and that the 'import os'
statement in MyClass.py would have been ignored.

Any comments or clues about how this SHOULD work would be appreciated!

TIA . . . .

Dan

Jul 18 '05 #1
3 1630
Dan Rawson wrote:
I would have expected that importing os from the interactive prompt
would have worked, and that the 'import os' statement in MyClass.py
would have been ignored.
every module has its own namespace.
Any comments or clues about how this SHOULD work would be appreciated!


the manual is a good place to start:

http://www.python.org/doc/current/tu...00000000000000
http://www.python.org/doc/current/ref/naming.html

</F>


Jul 18 '05 #2
Fredrik Lundh wrote:
Dan Rawson wrote:

I would have expected that importing os from the interactive prompt
would have worked, and that the 'import os' statement in MyClass.py
would have been ignored.

every module has its own namespace.

Any comments or clues about how this SHOULD work would be appreciated!

the manual is a good place to start:

http://www.python.org/doc/current/tu...00000000000000
http://www.python.org/doc/current/ref/naming.html

Hmm . . . I'm still a bit confused . . . The naming doc says that

from MyClass import MyClass

will only import the stuff that is part of the class scope (if I read it correctly). But it appears to also read and
use the 'import os' at the module level for the MyClass.py file.

The second question is why "import os" doesn't work at the interactive prompt; once I say that, isn't that a 'global' ??

Thanks!

Dan

Jul 18 '05 #3
Dan Rawson
Hmm . . . I'm still a bit confused . . . The naming doc says that

from MyClass import MyClass

will only import the stuff that is part of the class scope (if I read it correctly). But it appears to also read and use the 'import os' at the module level for the MyClass.py file.
The second question is why "import os" doesn't work at the interactive prompt; once I say that, isn't that a 'global' ??


Every module has its own namespace. The interactive prompt has its own
namespace, called "__main__". This is not the global namespace. (There is
a 'global' namespace called "__builtin__", but you should almost never put
anything into it because doing so most often indicates 'bad', or at least
non-standard/non-Pythonic programming style.)

All code in a module looks for so-called globals only in the module's
namespace
and, failing that, in the __builtin__ namespace. So your
MyClass.MyClass.show
method looks for 'os' first in the MyClass module and then in the
__builtin__,
finding it in neither.

This is what's often called "static scoping," as compared to "dynamic
scoping,"
which is what you are looking for. That wouldn't lead to easy to use code
because then when you import a class or function you would need to also
import all the modules it uses. I sure don't want to remember all those.

Andrew
da***@dalkescientific.com
Jul 18 '05 #4

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

Similar topics

3
by: Brad Tilley | last post by:
I don't understand classes very well... maybe some day. Is it just me or are they supposed to be difficult to understand? They make my head hurt. Anyway, because I don't understand classes well,...
18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
27
by: poiuz24 | last post by:
aeh, this may be stupid, but could s.b. please explain to me why the following won't link (gcc 3.3)? foo.cpp = """ namespace foo { int main () { return 0; }
3
by: Rubén Campos | last post by:
Organizing classes, types, structures, enums and whatever other entities into nested namespaces requires to include into every header and implementation file the complete path of namespaces. Let me...
3
by: Jim Heavey | last post by:
Trying to get the hang of Namespaces. I have primarly developed in VB and am transitioning to C# and the .Net Environment. I have worked a bit with Java as well in school about a year or so ago....
17
by: clintonG | last post by:
Using 2.0 with Master Pages and a GlobalBaseClass for the content pages. I understand the easy part -- the hierarchical structure of a namespace naming convention -- but the 2.0 IDE does not...
11
by: Random | last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I understand the purpose is so the developer can put classes within namespaces to essentially organize your code. And I...
4
by: michaeltorus | last post by:
H I've got an n-tier web app. The namespaces follow the layers in that I've got a DAL namespace, a BOL namespace and a WEB namespac What I'm tring to decide is the naming convention for my...
4
by: noosaj | last post by:
Hello, I use vs2005 and I'm a college student studying programming. I'm currently enrolled in a visual basic programming class. I have one question, and I apologize if it sounds dumb. What...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.