Please let me know if there are duplicated ones (I believe there are) and I will remove the post. I am just not sure about the jargon for this question.
When writing a new function augmented_func
which contains the other function already defined original_func
, I am trying to avoid defining arguments that are already in original_func
:
def original_func(a1, a2 ,a3):
print(a1, a2, a3)
def augmented_func(b, a1, a2, a3):
print(original_func(a1, a2, a3), b)
How do I avoid writing a
's when defining augmented_func
, and make it simpler especially when the number of arguments for original_func
is more than, say, three in the example?
Use *
notation for an arbitrary number of positional arguments:
def augmented_func(b, *args):
print(original_func(*args), b)
augmented_func('b', 1, 2, 3)
Or use keyword arguments with **
:
def augmented_func(b, **kwargs):
print(original_func(**kwargs), b)
augmented_func('b', a1=1, a2=2, a3=3)
I think what you looking for is something like
def original_func(a1, a2 ,a3):
print(a1, a2, a3)
def augmented_func(b, *args):
print(original_func(*args), b)
you can call it then as augmented_func(1, 2, 3, 4)
*args
and **kwargs
is a good way for passing the variable number of arguments and passing them to other internal function calls.
Python docs for further info.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.