导入模块时内存使用之间的差异 [英] Difference between memory usage when importing modules

查看:108
本文介绍了导入模块时内存使用之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道以下列方式导入模块时内存使用之间的区别是什么

I'd like to know whats is the difference between the memory usage when importing modules in these ways:

import Mod1
from Mod1 import *
from Mod1 import a,b,c

主要在前两个之间.

推荐答案

第一个使用最少的内存,因为它仅在模块作用域中创建一个名称.

The first uses the least memory since it only creates a single name in the module scope.

第二个使用最多(假设Mod1显式地或在__all__中包含的不仅仅是abc),因为重新创建了所有名称.

The second uses the most (assuming Mod1 contains more than just a, b, and c either explicitly or in __all__) since all names are recreated.

在所有三种情况下,整个模块都是导入并执行的,因此,如果您要节省大量内存,那不是您要优化的内容.

In all three cases the entire module is imported and executed, so if you're looking for large memory savings this is not what you want to optimize.

这篇关于导入模块时内存使用之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆