def foo(name, *args, **kwargs):
I need to remove the first two arguments of *args
in case its length (len(args)
) is greater than two. Is it possible to do that?
Here is why I need to do that: I've got the following function:
def foo(name, xml='my.xml', xsd='my.xsd', *otherfiles):
print('name: ' + name)
print('xml: ' + xml)
print('xsd: ' + xsd)
print(otherfiles)
I need to add an optional boolean parameter to the arguments of this function, without breaking the backward compatibility. So I change the function to this:
def foo2(name, *otherfiles, **kwargs):
kwargs.setdefault('relativePath', True)
if len(otherfiles)>0:
xml = otherfiles[0]
else:
kwargs.setdefault('xml', 'my.xml')
xml = kwargs['xml']
if len(otherfiles)>1:
xsd = otherfiles[1]
else:
kwargs.setdefault('xsd', 'my.xsd')
xsd = kwargs['xsd']
print('name: ' + name)
print('xml: ' + xml)
print('xsd: ' + xsd)
print(otherfiles)
Now I test the backward compatibility by checking if the output of foo
and foo2
is the same:
foo('my_name', '../my.xml', '../my.xsd', 'file1', 'file2')
print('===============================================================')
foo2('my_name', '../my.xml', '../my.xsd', 'file1', 'file2')
The output:
name: my_name
xml: ../my.xml
xsd: ../my.xsd
('file1', 'file2')
===============================================================
name: my_name
xml: ../my.xml
xsd: ../my.xsd
('../my.xml', '../my.xsd', 'file1', 'file2')
As you can see, the first two items of otherfiles
should be removed to maintain the old functionality.