Python modules

Chris Angelico rosuav at gmail.com
Mon Jan 14 16:04:00 CET 2013


On Tue, Jan 15, 2013 at 1:54 AM, zoom <zoom at yahoo.com> wrote:
> Is there any "rules" regarding importing python modules within your own
> module? I mean, how does this affects the performance of the program?
>
> In short, when creating a module, is it worthwhile to be careful and import
> only necessary functions, nothing more?

Nope. When you import a module, a record of it is kept in sys.modules,
so the next time you import it, it's just picking up the same module
object.

> scipy.r_[a] = sound.scipy.r_[a]

They'll actually be the same thing, which you can test with the 'is'
operator. The performance cost of reimporting a module is very low; in
fact, trying to avoid it by adorning all your usage with an extra
dot-level will probably cost you a lot more, since there'll be an
extra lookup every time.

Have at it! :)

ChrisA



More information about the Python-list mailing list