Python中单下划线变量的作用是什么?
What is the purpose of the single underscore variable in Python?
这一行在 python 代码中是什么意思(,_)?这是一条连接我的 PyQt UI 和我的 python 脚本
的线
FROM_CLASS, _ = loadUiType(path.join(path.dirname(__file__), "car_Proj.ui"))
没有这个,我得到了这个错误
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
从source code可以看出,loadUiType
returns两个对象:form_class
和base_class
。从您的代码来看,您似乎对 base_class
不感兴趣,因此将其命名为 _
,这是 "unimportant" 变量的约定。或者,您可以使用:
FROM_CLASS = loadUiType(path.join(path.dirname(__file__), "car_Proj.ui"))[0]
有关 python
中 _
约定的更多信息,请参阅 this answer
其实很简单:当loadUiType
return有两个值,但你只想存储一个供以后使用,你可以将一个赋值给虚拟变量_
。它也适用于不止两个 return 值 (_, my_var, _, _, _ = returns_five_values()
)。
这一行在 python 代码中是什么意思(,_)?这是一条连接我的 PyQt UI 和我的 python 脚本
的线FROM_CLASS, _ = loadUiType(path.join(path.dirname(__file__), "car_Proj.ui"))
没有这个,我得到了这个错误
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
从source code可以看出,loadUiType
returns两个对象:form_class
和base_class
。从您的代码来看,您似乎对 base_class
不感兴趣,因此将其命名为 _
,这是 "unimportant" 变量的约定。或者,您可以使用:
FROM_CLASS = loadUiType(path.join(path.dirname(__file__), "car_Proj.ui"))[0]
有关 python
中_
约定的更多信息,请参阅 this answer
其实很简单:当loadUiType
return有两个值,但你只想存储一个供以后使用,你可以将一个赋值给虚拟变量_
。它也适用于不止两个 return 值 (_, my_var, _, _, _ = returns_five_values()
)。