gpt4 book ai didi

python - 洗牌参数

转载 作者:行者123 更新时间:2023-11-28 20:47:44 25 4
gpt4 key购买 nike

我是 Python 的初学者,我正在看书。

所以这是一个模仿range()

的函数
def interval(start, stop=None, step=1):
'Imitates range() for step > 0'
if stop is None:
start, stop = 0, start # How is this evaluated?
result = []
i = start
while i < stop:
result.append(i)
i += step
return result

我的问题是:如何评估:

start, stop = 0, start 部分?

因为我知道参数应该这样计算:

5, stop = 0, 5(我知道我错了,但我需要你告诉我这部分是如何求值的)

最佳答案

表格

x, y = a, b

是一个多重赋值,即 well (but obtusely) documented .我给出的简单示例等同于

x = a
y = b

或者在您的示例中 start, stop = 0, start

stop = start
start = 0

注意我是如何重新排列作业的;这是多重分配的一个优点。在这种情况下,如果您想要相同的效果,则必须添加一个临时变量:

temp = start
start = 0
stop = temp

关于python - 洗牌参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18165296/

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