Trusted answers to developer questions

What are keyword arguments in Python?

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

In Python, the terms parameter and argument are used interchangeably. However, there is a slight distinction between these two terms. Parameters are the input variables bounded by parentheses when defining a function, whereas arguments are the values assigned to these parameters when passed into a function (or method) during a function call.

def team(name, project):
print(name, "is working on an", project)
team("FemCode", "Answers")

In the example above, the function arguments are FemCode and Answers, whereas name and project are the function parameters.

Types of Arguments

There are two types of arguments: positional arguments and keyword arguments.

Positional arguments

Positional arguments are values that are passed into a function based on the order in which the parameters were listed during the function definition. Here, the order is especially important as values passed into these functions are assigned to corresponding parameters based on their position.

def team(name, project):
print(name, "is working on an", project)
team("FemCode", "Answers")
Example of positional arguments

In these examples, we see that when the positions of the arguments are changed, the output produces different results. Though the code in example 2 isn’t wrong, the values that have been passed into the function are in the wrong order; thus, producing a result that does not match our desired output. Answers is the name of the project that is being worked on by the team, FemCode, not the other way around.

Now that the understanding is clear, let’s move on to keyword arguments.

Keyword arguments

Keyword arguments (or named arguments) are values that, when passed into a function, are identifiable by specific parameter names. A keyword argument is preceded by a parameter and the assignment operator, = .

Keyword arguments can be likened to dictionaries in that they map a value to a keyword.

def team(name, project):
print(name, "is working on an", project)
team(project = "Answers", name = 'FemCode')
Example of keyword arguments

As you can see, we had the same output from both codes although, when calling the function, the arguments in each code had different positions.

With keyword arguments, as long as you assign a value to the parameter, the positions of the arguments do not matter.

However, they do have to come after positional arguments and before default/optional arguments in a function call.

Default arguments are keyword arguments whose values are assigned at the time of function definition. Optional arguments are default arguments that, based on the values assigned to them (e.g., None or 0), can be disregarded.

def team(name, project, members=None):
team.name= name
team.project= project
team.members= members
print(name, "is working on an", project)
team(name = "FemCode", "Answers")
Code example for keyword arguments position in the function

The ‘Wrong’ code above generated a syntax error. In Python, a syntax error is basically an error that goes against a rule in Python. In terms of the above error, Python did not approve of the order in which the arguments were passed.

Consider this example:

def team(name, project):
print(number, name,"are working on an", project)
team("The two members of", "FemCode", "Answers")

The above code throws a TypeError that draws your attention to the number of allowed input arguments.

Is there a way around this such that we can pass in more arguments than is allowed? Let’s find out!

Fixed arguments vs. arbitrary arguments

Up until this point, we have been dealing with fixed function arguments. This simply means that the number of arguments we previously passed into functions were always limited to a specific quantity since we knew the number of arguments needed in advance. Sometimes, we do not know the number of arguments needed in advance; thus, we need to input more arguments than previously mentioned in the function definition. How do we go about this?

Python allows us to do this through certain special syntaxes that are collectively known as arbitrary arguments (or variable-length arguments). Here, unlike with fixed arguments, parameters are not specified by distinct individual names, but rather a general term to better encapsulate the shared attribute of the type of arguments being passed into the function. These syntaxes are of the forms:

  • *args: for non-keyworded/positional arguments
  • **kwargs: for keyworded arguments.

These labels, *args and **kwargs, represent the iterables that would be accessed during a function call.

A single asterisk signifies elements of a tuple, whereas double asterisks signify elements of a dictionary.

def team(*members):
for member in members:
print(member)
team("Abena", "Marilyn")
Code example for *args

Feel free to edit the code should you wish to add more arguments. You’ll see that executing the code will not throw a runtime error. This is because there is no limit to the number of arguments that can be passed into the function.

Note: The words, args and kwargs, used in this case are simply placeholders to better demonstrate a general collective name for each type of arbitrary argument stated during a function definition. This was done purposely to guide you in your choice of the number of asterisks to use when mentioning your parameters. When writing your code, you would specify a general term that would better capture the shared attribute of all elements in your iterable.

Below is an implementation of the combination of *args and **kwargs

def team(*members, **features):
for member in members:
print(member)
for key,value in features.items():
print("{}: {}".format(key,value))
team("Abena", "Marilyn", Name = "FemCode", Project = "Answers", Number = "Two Members")

In this same regard, we can pass a list and/or a dictionary into functions with arbitrary arguments to enable python to iterate through each and every element of an unspecified number of items in that iterable.

Hope you enjoyed the lesson!

RELATED TAGS

python
parameter
argument
arbitrary
Did you find this helpful?