gpt4 book ai didi

python - 参数默认使用python中的其他参数

转载 作者:太空宇宙 更新时间:2023-11-04 08:46:09 25 4
gpt4 key购买 nike

有没有可能在 Python 中做这样的事情?

def func(list, l = len(list)):
print list, l

最佳答案

通常是这样的

def func(list, l = None):
if l is None:
l = len(list)
print list, l

作为旁注,避免使用内置类型/函数名称作为变量名称(如此处的 list)

关于python - 参数默认使用python中的其他参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40354233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com