python模块引入的三种方式
python模块引入的三种方式
在使用模块时,我们根据不同的需求,会有三种引入方式,一是使用import语句,二是使用from...import...语句,三是使用from...import*语句。
1、使用import语句
2、from…import…语句
当我们只需要使用到模块中的部分资源时,全部导入未免过于消耗资源,这时我们可以使用from...import...语句。
假设my_demo.py模块中还定义了很多函数、变量,而我们只需要使用author这个变量。
#导入模块中的author变量
frommy_demoimportauthor
#使用author变量
print(author)
import…from…语句相当于导入模块中具体的资源,并将资源直接导入了当前的命名空间,使用时直接使用资源名称即可。
3、from…import*语句
当我们需要使用模块中所有的资源是,可以使用from...import*语句来实现。
#导入模块中的所有资源
frommy_demoimport*
#使用author变量
print(author)
#调用get_joke函数
joke_text=get_joke()
print(joke_text)
import…from*语句与import…from…类似,但不建议使用,使用这类语句会破坏命令空间的管理,会给我们带来一些麻烦。
以上就是python模块引入的三种方式,希望能对大家有所帮助。更多Python学习教程请关注IT培训机构:千锋教育。

猜你喜欢LIKE
相关推荐HOT
更多>>
python函数中使用for循环
python函数中使用for循环1、在for循环中使用函数需要更长的执行时间,因为每次迭代都会调用该函数。2、如果for循环是在函数内部实现的,那么该...详情>>
2023-11-14 13:53:34
python3.1版本的特性有哪些
python3.1中的特性有哪些1、千位数格式化,可以在使用字符串格式化函数时直接完成。在格式化大数时,通常是每三位数放置逗号,使数字更易读(例...详情>>
2023-11-14 13:18:27
python__new__()和__init__()有什么区别?
在python中,__new__()不是一定要有,只有继承自object的类才有,该方法可以return父类(通过super(当前类名,cls).__new__())出来的实例,或者直...详情>>
2023-11-14 12:38:55
pythonwheel是什么
python的第一个主流打包格式是.egg文件,现在大家庭中又有了一个叫做Wheel(*.whl)的新成员。wheel“被设计成包含PEP376兼容安装(一种非常接近于...详情>>
2023-11-14 11:30:39热门推荐
pythonSymPy求极值
沸python归并排序和快速排序比较
热pythonpartition如何分割字符串
热pythonif-elif-else语句的使用注意
新python函数中使用for循环
python3.1版本的特性有哪些
python__new__()和__init__()有什么区别?
python作为小白该如何抉择python编辑器?
pythonwheel是什么
python如何定义一个函数
pythonpython是什么类型的语言
python怎么传参数
pythonshell是什么
python如何查看对象属性
技术干货






