Is there a way to keep the Python 3 syntax for calling a default parameter after *args
while still being Python 2-compatible?
I have a function like so:
def foo(*args, bar='test'):
pass # do stuff with args and bar
The problem is the module needs to be compatible with both Python 2 and 3, therefore the default argument after *args
won't work. I've seen this answer, which recommends doing
def foo(*args, **kwargs):
bar = kwargs.pop('bar', 'test')
# do stuff with args and bar
Which works, but is undeniably less clear than the first method. The nice thing about the first method is it is partially self-documenting, whereas with the second one the user has to search around a bit to figure out what's going on. And Readibility counts.
So, is there a way I can at least have the first method in the module while still having the code run on Python 2.7? For example, possibly writing the function definition twice, once for Py2 and once for Py3?