python导入模块中函数或者变量的方法

本文介绍了Python中导入模块的多种方式,包括导入整个模块、导入特定函数或变量,以及使用as给模块和函数取别名。通过示例展示了不同导入方法的使用和运行结果。
摘要由CSDN通过智能技术生成

1、导入整个模块中的函数和变量,代码:
》创建模块say_hello.py文件:

#file:say_hello.py
def say_hi():
    print("hello,I'm ISmileLi!")

name = 'ISmileLi'

》使用模块say_hello.py如下:

#file:use_say_hello.py
import say_hello

say_hello.say_hi()
get_name = say_hello.name
print(get_name)

运行结果:
在这里插入图片描述

2、导入模块中的某个函数或者变量

#file:use_say_hello_func_attr.py

from say_hello import say_hi,name

say_hi()
print(name)

运行结果同1

3、导入模块中的所有函数和变量

#file:import_say_hello_all

from say_hello import *

say_hi()
print(name)

运行结果同1

4、导入模块时使用as给模块取别名

#file:give_mode_new_name.py

import say_hello as sh

sh.say_hi()
print(sh.name)

运行结果同1

5、导入函数时使用as给函数或者变量取别名
》函数取别名

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值