我想使用一个函数并传递一个参数给它是否存在变量。 如果变量不存在,那么我想使用带有参数默认值的函数。
到目前为止,我的代码如下所示:
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
我觉得有更好的方法来写这个,有什么建议吗?
您可以构造要传递的不同参数,然后使用参数解包,就像这样
args = (source, target, max_cor_dist, transformation) if transformation \
else (source, target, max_cor_dist)
o3d.pipelines.registration.registration_icp(*args).transformation
args = [source, target, max_cor_dist]
if transformation:
args.append(transformation)
transformation = o3d...registration_icp(*args).transformation