basic class question..

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Mon Nov 16 21:19:30 EST 2009


On Sun, 15 Nov 2009 16:26:59 -0800, Pyrot wrote:

> On 11월15일, 오후9시52분, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
>> Pyrot schrieb:
>>
>> > class rawDNA:
>> >    import string
[...]
> (Tthe core reason that I'm bothering with this at all is because I heard
> imports are costly(in time, space, processing power). If this turns out
> to be a non-issue, than my questions regarding Imports are all moot :->)

Importing a module is only costly the first time (in any session) that 
you do so. Modules are cached, so the second time it is relatively fast.


> I forgot to write about my second question which was:
> 
> what happens when I use the import statement within a class/function
> declaration?
> I'm thinking either
> 1) It imports during the class/function declaration 
> 2) It imports the first time a class/function call(x = rawDNA() ) occurs

It imports when the class/function statement is *run*.

def f():
    import math

The body of the function doesn't run until you call the function, so the 
import doesn't happen until you call f().

class K:
    import math
    def method(self):
        import string

The class definition runs when you create the class, which imports math. 
But string is only imported when you call K().method().

> But if it's 2) then is the import valid outside of the function/class?

No. The *name* "math" exists only inside the scope of the function, or 
class, and is not visible outside. 

>>> class K:
...     import math
...
>>> K.math
<module 'math' from '/usr/lib/python2.5/lib-dynload/mathmodule.so'>
>>> math
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'math' is not defined



> what happens when the last function reference is removed?(del x)

You wrote:

x = rawDNA()

Deleting x won't do anything to the class rawDNA, or to the module string 
which is referenced by the rawDNA class.

If you delete the rawDNA class, that won't do anything to the string 
module cached in sys.



> I respect your(or anyone who would like to help me) time, so all I ask
> is some kind of document or "Best practices" guide dealing all about
> "import".


99.9% of the time you don't need to worry about it. Just import the 
modules you want at the top level of your program, and be happy that it 
all works fine.

The other 0.1% of the time, you might have one or two good reasons for 
putting imports inside functions:

(1) Encapsulation: you want to import a module to use in a function, 
without making it visible to other functions.

(2) Just In Time importing. Imports at the top level slow down your 
program's start up. Normally, this is trivial and it is not worth the 
extra complexity to do something about it, but if you have a particularly 
expensive module which happens to be used only in one function which is 
rarely needed, then you can speed up your program for the majority of 
times by putting the import inside that one function.


However, putting imports inside functions which are then called by 
threads can lead to deadlocks. So you also have to balance the risk of 
that happening.



-- 
Steven



More information about the Python-list mailing list