python函数缺省值及程序打包方式

来源:互联网 发布:大数据毕业设计本科 编辑:程序博客网 时间:2024/05/29 19:29
函数缺省值的使用
#!/usr/bin/python''' test python file'''class TestClass(object):    def __init__(self,a=1,b=2):        self.aa = a        self.bb = b    def printDebug(self):        print("aa:",self.aa,"bb:",self.bb)if __name__ == '__main__':    testV = TestClass()    testV.printDebug()    testV = TestClass(5,6)    testV.printDebug()    testV = TestClass(6)    testV.printDebug()    testV = TestClass(b=6)    testV.printDebug()
输出结果:
aa: 1 bb: 2
aa: 5 bb: 6
aa: 6 bb: 2
aa: 1 bb: 6
注:关于if __name__ == '__main__':的作用,执行python test.py时满足条件,当import test时,__name__则为模块名。

包/子包、模块和类的定义
python中的几个概念:包/子包 模块 类
包:包含__init__.py的文件夹,其下可以包含多个模块或子包
模块:*.py文件去掉后缀名,即为模块名
类:即为定义的类类型,空代码块需要使用pass
导入模块方式:
import modulename
import modulename as mn
import packageA.packageB.modulename 或者
from packageA.packageB import modulename

python打包程序
1、生成exe文件
安装pip install pyinstaller
在相应的文件目录下,使用命令pyinstaller -F -w 文件名.py,则会生成包含相应exe的文件夹。
2、生成app文件
使用py2app,安装pip install py2app
命令名为py2applet