Question on class module import

M

monkeyboy

Hello,

I want to write a wrapper class around a windows dll. If I put the
class in a module "Refprop.py" then import the class where I want to
use it, does the whole module get read by the interpreter or just the
class code?...

For example if I say the following in "Refprop.py" does the code above
the class statement get run on an import or should the loadLibrary
call be made in the constructor...the code below appears to work OK, I
just want to be sure I understand what happens on an import call....

from ctypes import *
rp = windll.LoadLibrary("refprop.dll")

class refprop():
'''
REFPORP class
'''
nc = c_long(1)
ierr = c_long(0)
......
 
M

MRAB

monkeyboy said:
Hello,

I want to write a wrapper class around a windows dll. If I put the
class in a module "Refprop.py" then import the class where I want to
use it, does the whole module get read by the interpreter or just the
class code?...
[snip]
The whole module will be run.

In Python 'def' and 'class' aren't declarations, but statements, which
have the side-effect of creating the function or class and binding it to
a name in the enclosing namespace.

If a module contains:

print "The start"

class Foo():
pass

print "The finished"

and then you import it, it'll print "The Start", then create a class
called "Foo" within the module's namespace, then print "The end".
 
S

Steven D'Aprano

Hello,

I want to write a wrapper class around a windows dll. If I put the class
in a module "Refprop.py" then import the class where I want to use it,
does the whole module get read by the interpreter or just the class
code?...

Every import always involves a module. You can't import a class in
isolation. Even if you use the form "from module import object", the
entire module gets loaded first, in order to access the object.


For example if I say the following in "Refprop.py" does the code above
the class statement get run on an import

Only the first time you import it. After that, the module is cached.

or should the loadLibrary call
be made in the constructor...

Do you want to call loadLibrary every time you create an instance? Then
put it in the constructor.

Do you want to call loadLibrary only once? Then leave it were it is.


the code below appears to work OK, I just
want to be sure I understand what happens on an import call....

Python looks in sys.modules to see if the module has already been loaded.
If it has, it returns the module.

If not, it searches for the module. If it finds it, then it executes the
code in the module, caches the module object in sys.modules, and returns.
 

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

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,520
Latest member
KrisMacono

Latest Threads

Top