Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
import question
#1
Hi,
Just a thought, not a problem:
Say I have a prog that starts with 12 imports like shutil, datetime, warnings, pandas, a class of my own...etc

Some of these imports are only needed once:
eg. pandas is only used to read the contents of a spreadsheet right at the start, then no more.

Question1: is it good practice (from a memory point of view or ...) to do the import inside the def ...() where it is needed?
Question2: with question1 in mind, could there be a different answer idepending on what import it is.
Maybe pandas yes but math no, because it is already part of python.
?
Or is this much ado about nothing.
thx,
Paul
rob101 likes this post
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#2
(Aug-24-2022, 06:29 AM)DPaul Wrote: Question1: is it good practice (from a memory point of view or ...) to do the import inside the def ...() where it is needed?
In exceptional cases, there could be an advantage to do the import inside the function's body: if the function is never called during one execution of the program, the import is never done. For example your program could usually not need pandas, but in exceptional circumstances, it needs pandas because the user required some particular action.

For functions that are called often, there is a small penalty in writing the import in the function's body: a function call in the import system and a lookup in sys.modules every time your function's body is executed.

In almost all cases, these hair-splitting arguments will not lead to significant gain or penalty, so you'd better stick to writing the import statements at the top of the program. Premature optimization is the root of all evil.

DPaul Wrote:Question2: with question1 in mind, could there be a different answer idepending on what import it is.
If a module is already loaded every time the Python interpreter starts, there is no penalty of any kind to import this module at the top of the program. The module is already in memory and the import is simply a lookup in sys.modules.

More seriously I found one good reason to write import statements inside function's body in the past: when you want to write self-contained functions that don't need external symbols. For example this python2 function
def ieee754(x):
    """Return a string of 0 and 1 giving the ieee754 representation of the float x
    """
    import struct
    from binascii import hexlify
    p = struct.pack("d", x)
    s = bin(int(b"1" + hexlify(p), 16))[3:]
    return " ".join(reversed([s[i:i+8] for i in xrange(0, len(s), 8)]))
I can import a library containing my function ieee754() but 'struct' and 'binascii' are only imported if I use this function. If the library contains 50 such functions that may load 100 modules, this may avoid unnecessary imports.
rob101 likes this post
Reply
#3
(Aug-24-2022, 07:09 AM)Gribouillis Wrote: In almost all cases, these hair-splitting arguments will not lead to significant gain or penalty, so you'd better stick to writing the import statements at the top of the program.
Loud and clear!
thx,
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020