Python:“导入模块"与“导入模块为" [英] Python : 'import module' vs 'import module as'

查看:112
本文介绍了Python:“导入模块"与“导入模块为"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下两个语句之间是否有区别?

Is there any differences among the following two statements?

import os
import os as os

如果是这样,哪个更优选?

If so, which one is more preferred?

推荐答案

以下语法将帮助您了解在导入模块时使用"as"关键字的用法

The below syntax will help you in understanding the usage of using "as" keyword while importing modules

 import NAMES as RENAME from MODULE searching HOW

使用它可以帮助开发人员为导入的模块使用用户特定的名称. 示例:

Using this helps developer to make use of user specific name for imported modules. Example:

 import random 
 print random.randint(1,100)

现在,我想为随机模块介绍用户特定的模块名称 我可以将上面的代码重写为

Now I would like to introduce user specific module name for random module thus I can rewrite the above code as

 import random as myrand
 print myrand.randint(1,100)

现在问您的问题;首选哪一个? 答案是您的选择;使用"as"作为导入模块的一部分不会对性能造成任何影响.

Now coming to your question; Which one is preferred? The answer is your choice; There will be no performance impact on using "as" as part of importing modules.

这篇关于Python:“导入模块"与“导入模块为"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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