FAQ
Hello list,

I am wondering about loading modules.What is a good way of doing this?

In my code I got one single function, where I need some functionality
from a built-in module.

Is it a better way to load the module only within the function
pro: the function will only be hit every now and then or only with a
probability of 90%.
So in 90% cases the modules will not be loaded. (preserve
resources)

What is the balance between speed, resources and code style?

Is there a pythonian way for deciding in which case to do what?

Kind regards
Cornelius

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 262 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20110413/87dba6de/attachment.pgp>

Search Discussions

  • Peter Otten at Apr 13, 2011 at 4:02 pm

    Cornelius K?lbel wrote:

    I am wondering about loading modules.What is a good way of doing this?

    In my code I got one single function, where I need some functionality
    from a built-in module.
    built-in modules are always imported by definition, so you cannot save space
    or time by moving the import statement into the function. You can find the
    true built-in modules with

    sys.builtin_module_names

    and the modules that may have been imported indirectly by other modules you
    are using with

    sys.modules
    Is it a better way to load the module only within the function
    pro: the function will only be hit every now and then or only with a
    probability of 90%.
    So in 90% cases the modules will not be loaded. (preserve
    resources)

    What is the balance between speed, resources and code style?
    I doubt that you'll save a significant amount of space/time, and you make
    your module's dependencies less obvious.
    Is there a pythonian way for deciding in which case to do what?
    The pythonic approach is to measure; run your script with and without the
    tentative optimization and then decide whether the extra complication pays
    off.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedApr 13, '11 at 3:09p
activeApr 13, '11 at 4:02p
posts2
users2
websitepython.org

2 users in discussion

Peter Otten: 1 post Cornelius Kölbel: 1 post

People

Translate

site design / logo © 2022 Grokbase