Interview Prep/Python for DevOps/Language essentials & functions

What are `*args` and `**kwargs`, and how do you unpack them at the call site?

BeginnerConceptLanguage essentials & functions
Answer

*args collects extra positional arguments into a tuple; **kwargs collects extra keyword arguments into a dict. At a call site, * and ** unpack.

def run(cmd, *args, **kwargs):
    return subprocess.run([cmd, *args], **kwargs)

params = {"Filters": [{"Name": "tag:env", "Values": ["prod"]}]}
ec2.describe_instances(**params)

The **params pattern is common with boto3 because you build optional arguments conditionally and only pass the ones you have.

All Python for DevOps questions Roadmap: Python for automation

More on Language essentials & functions