[Tutor] Difference between >>> import modulex and >>> from modulex

Todd G. Gardner piir at earthlink.net
Sat Jan 3 14:59:18 EST 2004


Hello again,

Please see 'pydaq.py' below.  I will stop using 'from modulex import *' for
future code because I can see that can cause namespace conflicts and
confusion.  You all gave excellent answers that really help clear up some
confusion on this subject, so thanks.

1) Is there an easy way to change the legacy code I have to work if I change
'from modulex import foo' to 'import modulex'?

2) Is pydaq.__init__ executed if I write '>>> from pydaq import
AI_Configure'?

3) Is pydaq.__init__ executed if I write '>>> import pydaq'?

Thank you,

Todd

[BEGIN pydaq.py]
__________________________________________________________________
from Numeric import *
from ctypes import *
from string import *
from struct import *

# Create instance of traditional nidaq dll
ni=windll.nidaq32

class pydaq:
    def __init__(self):
        """variable declaration"""
        self.deviceNumber=c_int(1)
        self.chan=c_int(0)
        self.voltage=c_double(1.0)
        self.pvoltage=pointer(self.voltage)
        self.gain=c_int(-1)
        self.num=100 #Number of data points
        self.sampleRate=c_double(1000)
        self.max=10.0
        self.min=-10.0
        self.bits=12

    def AI_Configure (self, deviceNumber=c_int(1), chan=c_int(0),
inputMode=c_int(0), inputRange=c_int(10), polarity=c_int(0),
driveAIS=c_int(0)):
        """configure analong input task"""
        self.AI_ConfigureStatus = ni.AI_Configure (deviceNumber, chan,
inputMode, inputRange, polarity, driveAIS)
        #print "AI_Configure status =", self.AI_ConfigureStatus
        return self.AI_ConfigureStatus
[END pydaq.py]
__________________________________________________________________

-----Original Message-----
From: exnihilo at myrealbox.com [mailto:exnihilo at myrealbox.com]
Sent: Saturday, January 03, 2004 7:16 AM
To: Todd G. Gardner
Cc: Tutor at Python. Org
Subject: Re: [Tutor] Difference between >>> import modulex and >>> from
modulex


hi Todd,

Todd G. Gardner wrote:

>Hello Everyone,
>
>I have some questions that I must have skipped the answers in the tutorial.
>Pardon me if they are obvious.
>
>1) What is the difference between ">>> import modulex" and ">>> from
modulex
>import *"?
>
>
>
Others will certainly give you more definitive answers, but as I
understand it, the difference is in whether the contents of the module
get imported or the module as a whole gets imported into the current
namespace. This is clearer when you consider how you refer to what was
imported in the two cases.

If there were a function foo in modulex, then after doing "import
modulex", I call the function by doing something like "modulex.foo()".
The import added an entry for modulex to the symbol table for the
current module, so to access something in modulex, i do
"modulex.something".

If, on the other hand, I had done "from modulex import foo", then that
would have added foo to the current namespace (and not added an entry
for the module as in the previous example). The foo function would be
part of the current namespace, and i could call it with just "foo()"
(modulex.foo() would not work). The same goes for any kind of attribute
within the module i'm importing, whether they're functions, classes,
constants, or whatever.

Here is a little illustration (with some # comments):
 >>> import urllib2 # this doesn't import the contents of urllib2 into
the current namespace, it just adds an entry for the module to the
symbol table of this module
 >>> f = urllib2.urlopen('http://www.google.com')  # so i have to refer
to the urlopen function of the module as urllib2.urlopen
 >>> googlehtml = f.read()
 >>> f.close()
 >>> # print googlehtml would show all the html code i just downloaded
from google
If i used "from module import attribute", then it would be as follows:
 >>> from urllib2 import urlopen
 >>> f = urlopen('http://google.com') # i can now use urlopen unprefixed
by its module name
 >>> googlehtml = f.read()
 >>> f.close()

>2) What happens when the command reads ">>> from modulex import something"?
>    2a) Is something a function that is imported from modulex?
>
>
>
I'm not sure exactly what you're refering to in 2), but "from modulex
import something" would import into the current namespace whatever
something refers to (this will often be a class), and you would be able
to refer to it with just 'something' (without the quotes), instead of
modulex.something

I hope this helps. If not, don't worry, clearer explanations will follow...

-n.




More information about the Tutor mailing list