1

I want to use a function and pass an argument to it whether a variable exists. If the variable does not exist, then I want to use the function with the default value of the argument.

So far my code looks like this:

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

I feel like there is a better way to write this, any suggestions ?

mkr
  • 17
  • 6
  • What’s the default value if you don’t pass the argument? – deceze Dec 09 '22 at 09:00
  • @deceze sorry for not clarifying that. The default value is an identity matrix, of type numpy array with shape 4x4. Here is the [link](http://www.open3d.org/docs/latest/python_api/open3d.pipelines.registration.registration_icp.html#open3d-pipelines-registration-registration-icp) to the function I want to use in case anyone wants that. – mkr Dec 09 '22 at 09:04

2 Answers2

2

You can construct just the different arguments to be passed and then use argument unpacking, like this

args = (source, target, max_cor_dist, transformation) if transformation \
    else (source, target, max_cor_dist)
o3d.pipelines.registration.registration_icp(*args).transformation
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
2
args = [source, target, max_cor_dist]
if transformation:
    args.append(transformation) 
transformation = o3d...registration_icp(*args).transformation
matszwecja
  • 6,357
  • 2
  • 10
  • 17