我可以解开包装/破坏打字。

问题描述 投票:0回答:1
这是一个简单的问题,所以我很惊讶我找不到这样的问题(抱歉,如果我错过了),当我打算重构以命名tuple代替元组时,它总是浮现在我的脑海中.

我可以将Atyping.NamedTuple

解开作为参数或破坏性的作业,就像我可以使用A
tuple

python destructuring namedtuple argument-unpacking
1个回答
5
投票
你当然可以。

from typing import NamedTuple class Test(NamedTuple): a: int b: int t = Test(1, 2) # destructuring assignment a, b = t # a = 1 # b = 2 def f(a, b): return f"{a}{b}" # unpack f(*t) # '12' 包装顺序是定义中字段的顺序。


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.