- 位置パラメータ
- キーワード引数 (キー=値)
- デフォルトパラメータ(デフォルト値は最後に記述する必要があります)
- 不定長パラメータ(1:位置転送、2:キーワード転送)
- 関数はパラメータとして渡されます(データではなく計算ロジックを渡します)。
例:
# 参数传递参数
def user_info(name, age, gender):
print(f"姓名:{name},年龄:{age},性别:{gender}")
# 位置参数
user_info("小米", 18, "男")
# 关键字参数 ----- 键=值
user_info(name="小米", age=18, gender="男")
user_info(age=18, gender="男", name="小米")
user_info("小米", age=18, gender="男")
# 缺省参数 ------- 必须在最后
def user_info1(name1, age1, gender1="男"):
print(f"姓名:{name1},年龄:{age1},性别:{gender1}")
user_info1(name1="小米", age1=18)
user_info1(name1="小米", age1=18, gender1="女")
# 不定长参数 ------ 类型1:位置传递 类型2:关键字传递
# 位置传递不定长 ---- 元组的形式
def user_info2(*args): # 元组
print(args, type(args))
user_info2(1, 2, "你好", "hello")
# 关键字传递不定长 ---- 字典的形式
def user_info3(**kwargs): # 字典
print(kwargs, type(kwargs))
user_info3(a=1, b=2, c="你好", d="hello")
# 函数作为参数传递 ----- 计算逻辑的传递,而非数据的传递
def user_info4(compute):
result = compute(1, 2)
print(result,type(compute)) # 3 <class 'function'>
def compute(x, y):
return x + y
user_info4(compute)
複数の戻り値を返す関数を作成するにはどうすればよいですか? 戻り値の順序に応じて、複数の変数を対応する順序で書き込むだけで受信できます変数はカンマで区切られ、さまざまなタイプのデータ戻り値がサポートされています。
def r_test():
return 1, 2, 3
x, y, z = r_test()
print(x)
print(y)
print(z)