Writing Dynamic Python Functions: Magic of args and kwargs

Beginner #13: How arbitrary arguments can simplify your code and boost flexibility

M. Ramadhan
Informatics
Published in
9 min readOct 31, 2024

--

Photo by Godfrey Atima

Table of Contents

What Are Arbitrary Arguments?
Arbitrary Positional Arguments
How *args Works
Examples
Arbitrary Keyword Arguments
How **kwargs Works
Examples
Combining *args and **kwargs
Examples
Key Points
Advantages of Using *args and **kwargs
Disadvantages of Using *args and **kwargs
Summary
Reference

What Are Arbitrary Arguments?

Arbitrary arguments in Python allow a function to accept a variable number of arguments, meaning that you don’t need to know how many arguments will be passed ahead of time. This is particularly useful when writing flexible functions that can handle different numbers or types of inputs.

There are two types of arbitrary arguments:

  1. Arbitrary Positional Arguments (using *args)

--

--

Informatics
Informatics

Published in Informatics

Sharing facts, thoughts, experiences, and ideas related to informatics

M. Ramadhan
M. Ramadhan

Written by M. Ramadhan

I’m a database designer and developer, childhood in Menggala, living in Palembang.

No responses yet