Python_模块

1.模块设计及其功能描述查看:

(1)设计模块AIP(Application Programming Interface),此步骤无需写具体实现方法

'''
this module aim to calculate salaries of employees  #模块作用描述
'''
company = 'None'    #定义全局变量

def yearSalary(monthSalary):
    '''calculate annual salary base on statistics of monthly salary'''
    pass            #设计API中不需要写出具体实现方法

def daySalary(monthsalary):
    '''calculate daily salary base on statistics of monthly salary'''
    pass

(2)编码实现API中描述的功能

'''
this module aim to calculate salaries of employees  #模块作用描述
'''
company = 'None'                     #定义全局变量

def yearSalary(monthSalary):
    '''calculate annual salary base on statistics of monthly salary'''
    return monthSalary*12            #编写出具体实现方法

def daySalary(monthsalary):
    '''calculate daily salary base on statistics of monthly salary(in light of average working time)'''
    return monthsalary/22.5

(3)写测试代码(if __name__ == '__main__': 确保测试是在模块中进行)

'''
this module aim to calculate salaries of employees  #模块作用描述
'''
company = 'None'                     #定义全局变量

def yearSalary(monthSalary):
    '''calculate annual salary base on statistics of monthly salary'''
    return monthSalary*12            #编写出具体实现方法

def daySalary(monthsalary):
    '''calculate daily salary base on statistics of monthly salary(in light of average working time)'''
    return monthsalary/22.5

if __name__ == '__main__':           #编写测试代码,确实函数实现了响应功能(首先要确保测试在该模块中进行,而不是被调用的情况)
    print(yearSalary(3000))
    print(daySalary(3000))

(4)模块私有化--略

(5)打印模块描述信息和模块方法中的描述信息

import test
print(test.__doc__)             #打印模块的描述信息
print(test.daySalary.__doc__)   #打印模块中方法的描述信息

2.模块的导入

import test                 #导入一个模块
test.daySalary(30000)

import test,turtle          #同时导入多个模块
test.daySalary(30000)
turtle.Pen()

import test as t
t.daySalary(30000)          #导入一个模块并赋予其一个简称

from test import daySalary  #直接导入模块中的函数
daySalary(30000)            #直接可以使用方法

#动态导入
t = __import__('test')        #功能等同于import test as t
t.daySalary(30000)
#官方推荐的动态导入方法———通过importlib模块进行动态导入
import importlib
t = importlib.import_module('test')
t.daySalary(30000)

3.包的导入

#包里模块中方法的三种调用模式
import serve.easyTesti.print1
serve.easyTesti.print1.print1()     #①调用时需写完整名称

from serve.easyTesti import print1  #②直接导入模块
print1.print1()                     #直接调用模块中的方法

from serve.easyTesti.print1 import print1   #③直接导入方法
print1()                            #直接调用方法

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Krysertim

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值