定位:
玩转
函数是带有名称的代码块,完成特定的任务。
1、定义函数
可以定义一个由自己想要功能的函数,以下是简单的规则:
- 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号()。
- 任何传入参数和自变量必须放在圆括号中间。圆括号之间可以用于定义参数。
- 函数的第一行语句可以选择性地使用文档字符串—用于存放函数说明。
- 函数内容以冒号起始,并且缩进。
- return [表达式] 结束函数,选择性地返回一个值给调用方。不带表达式的return相当于返回 None。
#!/usr/bin/env
# encoding=utf-8
'''
- 我的调试专用demo
- create_date:2020-06-01
'''
import keyword
def sum(a, b):
'''
# 求和
# 学习python的方法语法和注释风格
:param a:
:param b:
:return:
'''
return a + b
if __name__ == '__main__':
print(keyword.kwlist)
sum = sum(10, 2)
print(sum)
2、传递实参
2.1 传递参数
还可以指定默认值,有一个好处就是固定数据类型
import keyword
def select_hero(hero_list=[], hero='妲己'):
'''
。和Java不同可以设置默认值
:param hero_list:
:param hero:
:return:
'''
hero_list.append(hero)
return hero_list
if __name__ == '__main__':
print("python编程要熟悉关键字")
print(keyword.kwlist)
heros = []
heros = select_hero(heros)
heros = select_hero(heros, "亚瑟")
heros = select_hero(hero_list=heros, hero="八戒")
heros = select_hero(heros, hero="程咬金")
heros = select_hero(heros, "孙悟空")
print(heros)
import keyword
def select_hero(hero_list=[], hero='妲己'):
'''
。和Java不同可以设置默认值
:param hero_list:
:param hero:
:return:
'''
hero_list.append(hero)
if __name__ == '__main__':
print("python编程要熟悉关键字")
print(keyword.kwlist)
heros = []
select_hero(heros)
select_hero(heros, "亚瑟")
select_hero(hero_list=heros, hero="八戒")
select_hero(heros, hero="程咬金")
select_hero(heros, "孙悟空")
print(heros)
上面两个实例,结果一样,是不是和Java又基本一致。对于列表,修改的是同一个对象值。
python目前没有学习真正的内存模型,Java 那么深入的堆栈分配,但是结果看差不多的处理逻辑。
import keyword
def select_hero(hero_list=[], hero='妲己'):
'''
。和Java不同可以设置默认值
:param hero_list:
:param hero:
:return:
'''
current_list = hero_list[:]
current_list.append(hero)
if __name__ == '__main__':
print("python编程要熟悉关键字")
print(keyword.kwlist)
heros = []
select_hero(heros)
select_hero(heros, "亚瑟")
select_hero(hero_list=heros, hero="八戒")
select_hero(heros, hero="程咬金")
select_hero(heros, "孙悟空")
print(heros)
上面这种方法操作列表才是安全的,函数不要对原始列表进行操作。
2.2、任意数量的实参
import keyword
def select_hero(hero_list=[], *hero):
'''
。和Java不同可以设置默认值
:param hero_list:
:param hero:
:return:
'''
current_list = hero_list[:]
for value in hero:
current_list.append(value)
return current_list
if __name__ == '__main__':
print("python编程要熟悉关键字")
print(keyword.kwlist)
heros = []
heros = select_hero(heros, "亚瑟", "妲己")
print(heros)
2.2、传递任意数量的键值对
def create_host(name, ip, **host_info):
'''
。创建主机我们看下键值对
:param hero_list:
:param hero:
:return:
'''
host = {"name": name, "ip": ip}
for key, value in host_info.items():
host[key] = value
return host
if __name__ == '__main__':
print("开始组装个主机")
hosts = create_host("dgg004-or", "10.22.159.63", type="EC2",flag = 0)
print(hosts)
3、将函数存在模块中
功能分区才是最合理的,把具有相同功能的function 放到一个独立的python文件中才是王道。
3.1 创建一个FunctionTest.py
#!/usr/bin/env
# encoding=utf-8
'''
- 我的调试专用demo
- create_date:2020-06-01
'''
def create_host(name, ip, **host_info):
'''
。创建主机我们看下键值对
:param hero_list:
:param hero:
:return:
'''
host = {"name": name, "ip": ip}
for key, value in host_info.items():
host[key] = value
return host
3.2 、创建一个TestClient.py
#!/usr/bin/env
# encoding=utf-8
'''
- 这个py 才是测试唯一入口
- 我的调试专用demo
- create_date:2020-06-01
'''
import FunctionTest as ft
if __name__ == '__main__':
print("开始组装个主机")
hosts = ft.create_host("dgg004-or", "10.22.159.63", type="EC2", flag=0)
print(hosts)
3.3 、只导入一个方法
上面是整个模块导入
from FunctionTest import create_host as ch
只导入一个方法
from FunctionTest import *
导入所有方法,不用模块. 可以直接使用方法名
不推荐使用,编程少写几个单词不叫简介,缺少必要有效信息增加了阅读的难度
#!/usr/bin/env
# encoding=utf-8
'''
- 这个py 才是测试唯一入口
- 我的调试专用demo
- create_date:2020-06-01
'''
from FunctionTest import create_host as ch
if __name__ == '__main__':
print("开始组装个主机")
hosts = ch("dgg004-or", "10.22.159.63", type="EC2", flag=0)
print(hosts)
今天又学习了一点知识:HAPPY
回到首页☞