Class18:初学python之模块和包

1、模块:

Python中一个.py文件就是一个模块

能避免函数名、变量名重名,增加代码的可读性

 

2、包:

新建package

每一个新建的包里都有一个__init__.py文件,每个模块都会执行一遍init里的代码。一般不建议删掉

往往是包里面存放着模块

 

二、导入

1、模块导入

a、导入内置模块

import time

print(time.time())

具体用法

Import time

date = time.strftime(‘%Y-%m-%d %H:%M:%S’)

print(date)

运行效果:  2020-06-18  181243

 

b、在模块2中导入模块1的函数名

方式一:

import test1

test1.foo()

test1.bar()

 

方式二:

导入单个函数

Form test1 import foo

Foo()

导入多个函数

form test1 import foo,bar

foo()

bar()

导入某个模块里的所有函数(不建议使用,需要哪个导入哪个)

from test1 import *

foo()

bar()

 

给函数起别名as

导入单个函数

Form test1 import foo as ooo

ooo()

 

2、包导入

导入指定模块

Import demo1.test1 as aaa

aaa.bar()

aaa.foo()

 

导入多个模块

import demo1.test1, demo1.test4

demo1.test1.foo()

demo1.test4.fun1()

导入包中模块的某个函数

from demo1.test1 import bar

bar()

导入包中模块的多个函数

from demo1.test1 import barfoo()

bar()

foo()

导入某个包中某个模块的所有函数

from demo1.test1 import *

bar()

foo()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值