Namespaces, classes, and using standard modules

D

Dan Rawson

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:

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
 
F

Fredrik Lundh

D

Dan Rawson

Fredrik said:
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
 
A

Andrew Dalke

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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top