question about imports in a class

Rhodri James rhodri at wildebst.demon.co.uk
Mon Dec 7 17:14:52 EST 2009


On Mon, 07 Dec 2009 21:13:25 -0000, J <dreadpiratejeff at gmail.com> wrote:

> Just a little newbie confusion about OS imports...
>
> Why does this give me an error:

It's generally helpful to say *what* error you get, including the  
traceback.  Fortunately the source code is enough this time.

> class Windows:
>
>     def __init__(self):
>         '''
>         Constructor
>         '''
>         import os
[snip the rest]

This is a bad idea for the reasons you explained yourself.  This import  
statement goes off and loads the module "os" and binds it to the name  
"os".  That name is local to Windows.__init__, just like any other name  
you'd bind here that wasn't explicitly declared to be global.  What you  
actually want is for "os" to be a global name; there are a whole bunch of  
insane ways of doing that, but the usual and least confusing thing to do  
is:

import os

class Windows:
     def __init__(self):
         # Do stuff...

[the next function was]
>    def parseDMI(self):
>        # First, find dmidecode.exe
>       for root,dirs,files in os.walk('C:\\'):
>            for file in files:
>                if file == "dmidecode.exe":
>self.dmidecodePath=[os.path.normcase(os.path.join(root,file))]
>                    return "Found DMIDecode.exe at %s" %  
> self.dmidecodePath
>                    break

I assume the formatting is the fault of some mailer somewhere trying to  
wrap the long line.  Such is life.

The break after the return statement is a bit pointless, though.  It's  
never going to get executed.

> Also, when I DO have the import statement inside the function
> definition, I'm screwing up the join and getting this:
> "C:\\dmidecod\\sbin\\dmidecode.exe"
>
> What am I doing that's creating the two \ characters??

At a guess, nothing.  Would I be right in thinking that what you actually  
get is ['C:\\dmidecod\\sbin\\dmidecode.exe'] ?  When you use string  
formatting with a list argument, the list turns itself into a string by  
calling repr() on its elements.  repr() on a string produces something  
that you could cut and paste into code as a string literal; to do that  
without changing the meaning of the string, it escapes the backslashes  
with another backslash.  You can play with this for yourself in the  
interactive shell:

rhodri at gnudebst:~$ python
Python 2.6.4 (r264:75706, Nov  2 2009, 14:44:17)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print r"hello \ world"
hello \ world
>>> s = r"hello \ world"
>>> print s
hello \ world
>>> print str(s)
hello \ world
>>> print repr(s)
'hello \\ world'
>>> a = [r"hello \ world"]
>>> a
['hello \\ world']
>>> print a
['hello \\ world']
>>> print "message = %s" % a
message = ['hello \\ world']
>>>

> So in my class, I import OS.  Now, in my calling script, do I also
> import OS, or will I inheirit OS as part of the class object?

Since you don't bind the name "os" into your class (and you shouldn't,  
since that would definitely be a code smell), you aren't going to inherit  
it.  Just import it where you need to and stop trying to make your life  
difficult!  It doesn't in fact cost you anything, since Python caches the  
module the first time you load it precisely to avoid having to go back to  
the disc every time you want to reload it.

-- 
Rhodri James *-* Wildebeest Herder to the Masses



More information about the Python-list mailing list