多变量声明 [英] Multiple variable declaration

查看:122
本文介绍了多变量声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中看到了这个声明,但是我不明白这意味着什么,也找不到解释:

I saw this declaration in Python, but I don't understand what it means and can't find an explanation:

ret, thresh = cv2.threshold(imgray, 127, 255, 0)

问题是:为什么retthresh之间会有逗号?那是什么类型的作业?

The question is: why is there there a comma between ret and thresh? What type of assignment is that?

推荐答案

这是一个元组" 解构" 作业-参见例如多种分配语义. cv2.threshold返回一个包含两个值的元组,因此等效于:

That's a "tuple" or "destructuring" assignment - see e.g. Multiple assignment semantics. cv2.threshold returns a tuple containing two values, so it's equivalent to:

temp = cv2.threshold(...)
ret = temp[0]
thresh = temp[1]

请参见语言参考中的赋值声明:

如果目标列表是目标的逗号分隔列表:该对象必须是可迭代的,并且具有与目标列表中存在目标的项目数量相同的项目,并且将这些项目从左到右分配给相应的目标.

If the target list is a comma-separated list of targets: The object must be an iterable with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets.

这篇关于多变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆