So you got a cool API with lots of magic? Then suddenly you wanted something like dynamically passing parameters when needed. A common pattern wherein you call an arbitrary method with arbitrary arguments. I know this is trival but if you come from another language, you probably don’t know about this.
Example
For example you have a function API below:
def is_email(value, message=None)
You normally call the function like below:
is_email(some_email, error_messages)
Unpacking arguments
You can pass a list as argument but unpack it *args
to become arguments like below:
args = ['some@email.com', 'Please enter a valid email address'] is_email(*args)
You can do the same for keyword arguments by using **args
notation.
args = {'email': 'some@email.com', 'message': 'Please enter a valid email address'} is_email(**args)
Why?
Unpacking arguments is useful when dealing with dynamic method calls. I’ll provide examples on my next post about validations.
Source: Python documentation.