Python 抽象
1、创建函数
关键字 def
2、文档化函数
3、参数
1、关键字参数和默认值
def hello_1(greeting, name):
print('%s , %s!' % (greeting, name))
// name 默认值 Tome11 greeting默认值hello
def hello_2(name='Tome11', greeting='hello'):
print('%s , %s!' % (greeting, name))
hello_1('hello', 'word')
//传值
hello_2('Tome', 'hello')
//使用默认值
hello_2()
//greeting默认值hello name为Luce
hello_2(name='Luce')
2、搜集参数 *参数名 表示可以传多个,打印出来是元组
def request(*params):
print(params)
request("code")
request("code", "name")
结果是:
('code',)
('code', 'name')
def request1(param, *params):
print(param)
print(params)
request1("RequestParams", "name")
request1("RequestParams", "name", "code", "age")
结果是:
RequestParams
('name',)
RequestParams
('name', 'code', 'age')
// 传字典
def request3(**params):
print(params)
request3(name="botter", age=30)
结果是:
{'name': 'botter', 'age': 30}
3、搜集参数的逆过程
def add(x, y):
return x + y
parm = (1, 3)
print(add(*parm))
结果是:4
网友评论