如何仅当变量存在时才将变量作为参数传递给函数

问题描述 投票:0回答:2

我想使用一个函数并传递一个参数给它是否存在变量。 如果变量不存在,那么我想使用带有参数默认值的函数。

到目前为止,我的代码如下所示:

if transformation:
    '''
    If there is a computed transformation
    take it as an initial value.
    '''
    transformation = o3d.pipelines.registration.registration_icp(source,
                                                                    target,
                                                                    max_cor_dist,
                                                                    transformation).transformation
else:
    '''
    If there is not a computed transformation
    do not take an initial value.
    '''
    transformation = o3d.pipelines.registration.registration_icp(source,
                                                                    target,
                                                                    max_cor_dist).transformation

我觉得有更好的方法来写这个,有什么建议吗?

python function arguments o3d
2个回答
2
投票

您可以构造要传递的不同参数,然后使用参数解包,就像这样

args = (source, target, max_cor_dist, transformation) if transformation \
    else (source, target, max_cor_dist)
o3d.pipelines.registration.registration_icp(*args).transformation

2
投票
args = [source, target, max_cor_dist]
if transformation:
    args.append(transformation) 
transformation = o3d...registration_icp(*args).transformation
© www.soinside.com 2019 - 2024. All rights reserved.