Python3 函数前 '*' 的用途

Purpose of '*' in front of Python3 function

我在 Python3 和 PyTorch here 中看到了 ResNet CNN 的代码如下:

def resnet_block(input_channels, num_channels, num_residuals,
                 first_block=False):
    blk = []
    for i in range(num_residuals):
        if i == 0 and not first_block:
            blk.append(Residual(input_channels, num_channels,
                                use_1x1conv=True, strides=2))
        else:
            blk.append(Residual(num_channels, num_channels))
    return blk

添加模块,使用如下代码-

b2 = nn.Sequential(*resnet_block(64, 64, 2, first_block=True))
b3 = nn.Sequential(*resnet_block(64, 128, 2))
b4 = nn.Sequential(*resnet_block(128, 256, 2))
b5 = nn.Sequential(*resnet_block(256, 512, 2))

什么是“*resnet_block()”mean/do?

基本上*iterable用于将可迭代对象的项目解包为位置参数。在您的问题中 resnet_block returns 一个列表,该列表的项目被传递给 nn.Sequential 而不是列表本身。