Education logo

Functions, 'args' and 'kwargs' in Python

A simple but functional explanation of functions and arguments in Python

By Estefanía NoguerónPublished 3 months ago 4 min read
Like
Functions, 'args' and 'kwargs' in Python
Photo by Will Porada on Unsplash

As we already know, a function is a subprogram or subroutine part of the main algorithm. The goal of functions, both in Python and other programming languages, is modularizing the code, making it much easier to organize, read, and reuse.

Depending on whether the function has input parameters or not, the lexical definition of a function in Python is as follows. Always preceded by the reserved word 'def'.

As we can see, the parameters are simple objects that are passed to a function and these can be of any type (strings, bool, int, double, floats, another function, etc).

The definition of these parameters can be simple (only indicating the name of the object to which the value will be assigned, as indicated in example #1), we can define it as a key-value (example #2) indicating a default value, and this becomes an optional value when calling the function, since if you omit it, it will take the default value:

As we can see, when calling the function, the order in which we call the parameters does not matter. Also, we can explicitly declare what type of data it is going to contain (example #3).

Although the explicit declaration is not necessary (since Python automatically infers the data type), I recommend doing it as a good practice. We will have a more self-explanatory and easy to read code.

What if we want to make sure that the parameters are passed in the order that they were declared in the function? We can make use of the '/' character to indicate that the beginning of the definition up to that character are only usable positionally (that is, only in the order they were declared). Parameters after the / symbol may be swapped.

Note: you cannot use parameter names when you want to pass positional arguments.

In Python, all arguments are passed by reference. This means that only the pointers of the variables (that is, their address in memory) are copied, not the content of the object itself. If you don't know what I mean, take a look at this article related to memory management in Python where I explain it.

The fact that the arguments are passed by reference has a great advantage: working with very large objects is very light on memory level, however, we must be very careful with mutable objects. If the argument is mutable and its content is changed inside the function, any references to that object outside the function will also be changed.

In the following example, we have two lists (mutable objects) and we modify one of them (men) in the user_datebase function. The content of the 'men' object will be modified:

However, if the objects were not mutable, for example a tuple, the content of the object (men_1) would not have been modified:

Note: due to the above problem, when using default values on objects that are mutable, it is convenient to initialize them with 'None'

Using 'args' and 'kwargs'

We have seen that the arguments can be passed positionally or by key-value, now we will see the nomenclature used and accepted by the community to identify them.

• Positional arguments are decomposed using the '*' character and are called 'args'(arguments)

• Arguments passed as key-values are parsed using the '**' characters and are called 'kwargs' (keyword arguments).

Note: key-value arguments always follow positionals.

The content of the parameters using args and kwargs is uncertain until the function call is made.

The use of args and kwargs is very useful when extending classes (inheritance) or executing functions that make use of some parameters that are provided only in the original call, since, in Python, function parameters are strict. and if it is not defined in this way, the functions will raise exceptions when trying to execute them with more or fewer parameters than those defined.

However, we must be very cautious when using them, since by doing implicit programming our code loses readability and makes the code poorly maintainable, and can even make teamwork difficult. From my point of view, the ideal is to explicitly name the parameters to use in each function.

I hope this article has been useful to you, if so, please leave me a 'like', comment, and click 'Follow', your support is very important to me. Cheers!

Estefana

student
Like

About the Creator

Estefanía Noguerón

Developer and writer about educational content of programming and data analysis with Python. I am passionate about Machine and Depp Learning.

More about me here: https://www.linkedin.com/in/estefan%C3%ADa-noguer%C3%B3n-90770915a/

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2023 Creatd, Inc. All Rights Reserved.