Keyword argument list python

What are keyword arguments in Python?

Many candidates are rejected or down-leveled in technical interviews due to poor performance in behavioral or cultural fit interviews. Ace your interviews with this free course, where you will practice confidently tackling behavioral interview questions.

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", "Edpresso")

In the example above, the function arguments are FemCode and Edpresso , 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", "Edpresso")

In these examples, we see that when the positions of the arguments are changed, the output produces different results. Though the code in example B 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. Edpresso is the name of the project that is being worked on by the team, FemCode, not the other way around.

Читайте также:  Python перевести дату в строку

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 = "Edpresso", name = 'FemCode')

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", "Edpresso")

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.

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

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:

Источник

Оцените статью