Namespaces, classes, and using standard modules

Discussion in 'Python' started by Dan Rawson, Aug 13, 2003.

  1. Dan Rawson

    Dan Rawson Guest

    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
     
    Dan Rawson, Aug 13, 2003
    #1
    1. Advertising

  2. Fredrik Lundh, Aug 13, 2003
    #2
    1. Advertising

  3. Dan Rawson

    Dan Rawson Guest

    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/tut/node8.html#SECTION008100000000000000000
    > 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
     
    Dan Rawson, Aug 13, 2003
    #3
  4. Dan Rawson

    Andrew Dalke Guest

    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
     
    Andrew Dalke, Aug 13, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan Williams

    Modules, namespaces, parents, and import

    Dan Williams, Feb 4, 2004, in forum: Python
    Replies:
    1
    Views:
    339
    Dang Griffith
    Feb 4, 2004
  2. Dietrich Epp
    Replies:
    6
    Views:
    381
  3. Brad Tilley

    classes and standard modules

    Brad Tilley, Sep 17, 2004, in forum: Python
    Replies:
    3
    Views:
    284
    Istvan Albert
    Sep 17, 2004
  4. Mage

    modules and namespaces

    Mage, Apr 19, 2005, in forum: Python
    Replies:
    0
    Views:
    271
  5. Laszlo Zsolt Nagy

    Re: modules and namespaces

    Laszlo Zsolt Nagy, Apr 19, 2005, in forum: Python
    Replies:
    1
    Views:
    298
    =?iso-8859-15?q?Berthold_H=F6llmann?=
    Apr 19, 2005
Loading...

Share This Page