加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

依赖Python函数参数评估顺序是否安全? [重复]

发布时间:2020-12-14 00:51:47 所属栏目:百科 来源:网络整理
导读:参见英文答案 Is Python’s order of evaluation of function arguments and operands deterministic (+ where is it documented)?2个答案假设在Python中从左到右计算函数参数是否安全? 参考说明它发生的方式,但也许有一些方法来改变这个可能会破坏我的代
参见英文答案 > Is Python’s order of evaluation of function arguments and operands deterministic (+ where is it documented)?2个答案假设在Python中从左到右计算函数参数是否安全?

参考说明它发生的方式,但也许有一些方法来改变这个可能会破坏我的代码的顺序。

我想要做的是为函数调用添加时间戳:

l = []
l.append(f(),time.time())

我知道我可以按顺序评估参数:

l = []
res = f()
t = time.time()
l.append(res,t)

但它看起来不那么优雅,所以如果我可以依赖它,我更喜欢第一种方式。

是的,Python总是从左到右评估函数参数。

据我所知,这适用于任何逗号分隔列表:

>>> from __future__ import print_function
>>> def f(x,y): pass
...
>>> f(print(1),print(2))
1
2
>>> [print(1),print(2)]
1
2
[None,None]
>>> {1:print(1),2:print(2)}
1
2
{1: None,2: None}
>>> def f(x=print(1),y=print(2)): pass
...
1
2

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读