《head first python》— 函数定义、模块与发布、共享代码

前端之家收集整理的这篇文章主要介绍了《head first python》— 函数定义、模块与发布、共享代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.定义一个函数

函数已经发布,则升级时,为新添加的变量设定缺省值的设定可以保证不同版本函数的兼容。

  1. 输出列表(包含嵌套和非嵌套列表),一次显示一行,嵌套列表可以缩进
  2. indent:是否缩进;leveltab缩进个数;fh为写入文件地址,默认显示到屏幕
  3. """
  4. for each_item in the_list:
  5. if isinstance(each_item,list):
  6. print_lol(each_item,indent,level+1,fh)
  7. else:
  8. if indent:
  9. for tab_stop in range(level):
  10. print("\t",end='',file=fh)
  11. print(each_item,file=fh)
2.函数转化为模块

函数保存到一个适当命名的文件中nester.py.

3.模块发布

文件夹命名为文件,包含发布的元数据。首先从python发布工具导入“setup”函数

  1. setup(
  2. name = 'nester',version ='1.0.0',py_modules =['nester'],auther ='a'
  3. auther_email ='a@s'
  4. description =''
  5. )

4.构建发布

功能,打开cmd命令行,定位到nester文件夹,输入:


setup.py install


文件夹。

5.上传到PyPI共享

<p style="font-size:13.3333339691162px;"><span style="font-family:'FangSong_GB2312';">linux:python2.7 setup.py sdist upload


<p style="font-size:13.3333339691162px;"><span style="font-family:'FangSong_GB2312';">windows:C:\Python27\python.exe <span style="font-size:13.3333339691162px;">setup.py sdist upload


<p style="font-size:13.3333339691162px;"><span style="font-family:'FangSong_GB2312';"><span style="font-size:13.3333339691162px;">6.debug过程


<p style="font-size:13.3333339691162px;"><span style="font-family:'FangSong_GB2312';"><span style="font-size:13.3333339691162px;">import模块报错,主要有以下错因:


<p style="font-size:13.3333339691162px;"><span style="font-family:'FangSong_GB2312';"><span style="font-size:13.3333339691162px;">(1)汉字注释。解决:开头添加#coding: utf-8


<p style="font-size:13.3333339691162px;"><span style="font-family:'FangSong_GB2312';"><span style="font-size:13.3333339691162px;">(2)书上用python3,实际用python2.7.在同行输出有问题


<p style="font-size:13.3333339691162px;"><span style="font-family:'FangSong_GB2312';"><span style="font-size:13.3333339691162px;"><span style="border:0px;font-size:15px;color:rgb(46,46,46);font-family:'Microsoft YaHei','宋体','Myriad Pro',Lato,'Helvetica Neue',Helvetica,Arial,sans-serif;line-height:26.6666679382324px;">Python
2

  1. Python 2.7.6
  2. Hello,World!
  3. Hello,World!
  4. text print more text on the same line
<span style="border:0px;font-size:15px;color:rgb(46,sans-serif;line-height:26.6666679382324px;">Python 3
<span style="border:0px;font-family:'Microsoft YaHei',sans-serif;">
  1. <code class="language-python">print('Python',python_version())
  2. print('Hello,World!')
  3. print("some text,",end="")
  4. print(' print more text on the same line')

  5. 结果

  6. Python 3.4.1

  7. Hello,World!

  8. some text,print more text on the same line

  9. print 'Hello,World!'

  10. 报错

  11. File "",line 1

  12. print 'Hello,World!'

  13. ^

  14. SyntaxError: invalid Syntax

<span style="border:0px;font-size:15px;color:rgb(46,sans-serif;line-height:26.6666679382324px;">(3)NameError:
name 'sys' is not defined<span style="color:#2e2e2e;"><span style="font-size:15px;line-height:26.6666679382324px;">解决:import sys<span style="color:#2e2e2e;"><span style="font-size:15px;line-height:26.6666679382324px;">代码更改为:<span style="border:0px;font-size:15px;color:rgb(46,sans-serif;line-height:26.6666679382324px;">
  1. <code class="language-python">
<pre name="code" class="python">#coding: utf-8
import sys
def print_lol(the_list,fh=sys.stdout):
"""
格式化输出列表(包含嵌套和非嵌套列表),一次显示一行,嵌套列表可以缩进
indent:是否缩进;level:tab缩进个数;fh为写入文件地址,默认显示到屏幕
"""
for each_item in the_list:
if isinstance(each_item,fh)
else:
if indent:
for tab_stop in range(level):
file=fh
print "\t",;print file
file=fh
print(each_item,file)

导入成功:


猜你在找的Python相关文章