Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我在 python 中有一个函数,它返回一对(文本,rc)。在某些情况下,我想使用这对的两个部分,但在某些情况下,我只想使用其中一个。在python中,是否有与以下类似的语法
text, rc = f() # read both, text and rc text, ~ = = f() # read only text ~, rc = = f() # read only rc
一个常见的 Python 习惯用法是_用作虚拟变量:
_
text,rc = f() text,_ = f() _,rc = f()
或者你可以只使用:
text,rc = f() text = f()[0] rc = f()[1]
你甚至可以使用多个_:
_,_,_,x = method_returning_4_args_and_only_want_the_last()