对Python函数设计规范详解
作者:Field_Yang 时间:2023-08-02 15:59:17
Python函数的设计规范
1、Python函数设计时具备耦合性和聚合性
1)、耦合性:
(1).尽可能通过参数接受输入,以及通过return产生输出以保证函数的独立性;
(2).尽量减少使用全局变量进行函数间通信;
(3).不要在函数中直接修改可变类型的参数;
(4).避免直接改变定义在另外一个模块中的变量;
2)、聚合性:
(1).每个函数都应该有一个单一的、目的统一的目标;
(2).每个函数的功能都应该相对简单;
2、Python函数在脚本中应用示例
例1:将/etc/passwd文件中的每一行都分隔为一个列表
[root@test0528]# vim test1.py
#!/usr/bin/python27
#
importre
filename ='/etc/passwd'
f1 =open(filename,'r')
l1 =f1.readlines()
bash =[]
for i inl1:
bash.append(i)
defgenList(x):
y = 0
x = len(bash)
while y <= x:
yield bash[y]
y += 1
g1 =genList(bash)
count =0
whilecount < len(bash):
gg=g1.next()
linelist = gg.split(':')
print linelist
count += 1
f1.close()
[root@test0528]# ./test1.py
['root','x', '0', '0', 'root', '/root', '/bin/bash\n']
['bin','x', '1', '1', 'bin', '/bin', '/sbin/nologin\n']
['daemon','x', '2', '2', 'daemon', '/sbin', '/sbin/nologin\n']
......
['nginx','x', '496', '493', 'nginx user', '/var/cache/nginx','/sbin/nologin\n']
['mysql','x', '27', '27', 'MySQL Server', '/var/lib/mysql','/bin/bash\n']
['redis','x', '495', '492', 'Redis Database Server', '/var/lib/redis','/sbin/nologin\n']
例2:将任意文件按用户指定的分隔符把每一行都分隔为一个列表
[root@test0528]# vim test2.py
#!/usr/bin/python27
#
importre
#print"PLease input filename:"
#filename= raw_input()
filename =str(raw_input("PLease input filename: "))
f1 =open(filename,'r')
l1 =f1.readlines()
#print"PLease input separator:"
#separator= raw_input()
separator= str(raw_input("PLease input separator: "))
bash =[]
for i inl1:
bash.append(i)
defgenList(x):
y = 0
x = len(bash)
while y <= x:
yield bash[y]
y += 1
g1 =genList(bash)
count =0
whilecount < len(bash):
gg=g1.next()
linelist = gg.split(separator)
print linelist
count += 1
f1.close()
[root@test0528]# ./test2.py
PLeaseinput filename: /etc/passwd
PLeaseinput separator: :
['root','x', '0', '0', 'root', '/root', '/bin/bash\n']
['bin','x', '1', '1', 'bin', '/bin', '/sbin/nologin\n']
['daemon','x', '2', '2', 'daemon', '/sbin', '/sbin/nologin\n']
...
['nginx','x', '496', '493', 'nginx user', '/var/cache/nginx','/sbin/nologin\n']
['mysql','x', '27', '27', 'MySQL Server', '/var/lib/mysql','/bin/bash\n']
['redis','x', '495', '492', 'Redis Database Server', '/var/lib/redis','/sbin/nologin\n']
例3:用折叠的方式(reduce)求阶乘
[root@test0528]# vim test3.py
#!/usr/bin/python27
# getn!
num =int(raw_input('please nput a number:'))
num +=1
list =range(1,num)
deffunc(m,n):
return m*n
x =reduce(func,list)
printx
[root@test0528]# ./test3.py
pleasenput a number:4
24
来源:https://blog.csdn.net/Field_Yang/article/details/80587895
标签:Python,函数,设计,规范


猜你喜欢
MySQL中Replace语句用法实例详解
2024-01-15 03:26:28
如何利用Python监控别人的网站
2022-08-11 16:54:18
基于python代码批量处理图片resize
2022-03-18 23:06:58
基于python traceback实现异常的获取与处理
2022-04-05 09:59:32
python编写脚本之pyautogui的安装和使用教程
2021-06-17 09:48:11

python引入requests报错could not be resolved解决方案
2022-08-28 08:24:45

学习python需要有编程基础吗
2022-05-01 15:23:47
Python 中的函数装饰器和闭包详解
2021-08-03 17:52:40

2008年Logo设计10大趋势
2008-02-28 13:06:00

Python时间序列处理之ARIMA模型的使用讲解
2021-04-10 05:53:34

DataFrame 数据合并实现(merge,join,concat)
2022-03-28 04:24:02
python3中dict.keys().sort()用不了的解决方法
2023-08-16 11:33:54

原生JS实现匀速图片轮播动画
2024-06-07 15:28:15

Python获取指定网段正在使用的IP
2022-04-22 23:49:27

python机器人行走步数问题的解决
2023-12-24 23:26:05
Python读写txt文本文件的操作方法全解析
2021-08-07 04:06:20

python小项目之五子棋游戏
2022-07-12 06:24:23

python3利用Socket实现通信的方法示例
2022-04-10 03:09:04

SQL Server 数据页缓冲区的内存瓶颈分析
2012-08-21 10:49:11
Python单元和文档测试实例详解
2023-01-01 19:08:06