https://www.seekpng.com/

Python: Argument

Sean
NTUST-AIVC
Published in
6 min readMay 12, 2022

--

Co-Author: Y. S. Huang, a master’s student studying AIVC, likes open-source.
If you are interested, go to check my Github!

This article will tell you more details about function in Python including
1. Argument
2.
The meaning of * and **before the variable
3.
Special parameter
If you want to check basic knowledge about Python, you can browse the following website:

Argument

In Python, when you call a function, you can pass some value or variable into the function with arguments. But you will meet a problem, what properties does the argument have and what kind of data can be taken by arguments?

What type of data can be treated as an argument?

Everything in Python is object.

In python, arguments defined in the function can receive OBJECT.
The arguments which received items would be available in the function called by other functions. No matter whether you want to pass variable, tuple, list, or dictionary even though function or class, those all are objects and can be treated as arguments. This is an important property that you should never forget.

Argument type

Positional argument & Keyword argument
The difference between positional and keyword is the way to assign the value of the argument when calling a function.

Positional argument:
Directly set a value or variable to a particular place in the argument list
when the user calls a function. This method is very careful about the order in the argument list. The interpreter will pass the particular value to the argument which has the identical place. For instance:

# Result
pos1 = 20
pos2 = 10
pos1 = 10
pos2 = 20

Keyword argument:
Directly set parameters equal to a value or variable.
key=value
This method doesn’t care about the order in the argument list. It cares about the key assigned value, not the order. Let’s check an example:

# Result 
pos1 = 10
pos2 = 20
pos1 = 10
pos2 = 20

This time, also called func twice but you get the same value in pos1 and pos2. That means function just cares about what key has been passed value and what value it is. It doesn’t matter the order you assign value in the argument list.

Number of argument

The function must be called with the correct number of arguments which also can be called as required arguments. You must meet this requirement, or you will get an alert.

Default parameter value

You can assign a value to parameters in the function definition. When function was called, you can pass no value to those parameters which have been assigned a default value. Those parameters which have default value are also called optional arguments. For instance:

# Result
pos1 = pos1 no val
pos2 = pos2 no val
pos1 = 10
pos2 = 11
pos1 = 10
pos2 = pos2 no val
pos1 = pos1 no val
pos2 = 11

Arbitrary argument ( *args )

When you don’t know how many positional arguments will be passed into the function, the Arbitrary argument allows you to use an undetermined number of positional arguments. When you call a function with lots of arguments, the Arbitrary argument will package the rest of the positional arguments to a tuple after all of the required arguments have been assigned value. So later you can use the Arbitrary argument in the function. For example:

# Result
pos1 = 1
pos2 = 2
args = (‘three’, 4.44, {‘e’: ‘E’})

As you can see, when call the func with argument list 1 , 2 , 'three' , 4.44 , {'e':'E'} , integers 1 and 2 are independently assigned to pos1 and pos2 .Rest of the positional arguments are packaged into a tuple by *args. When you print args, you can get the result shown above.

Arbitrary keyword argument ( **kwargs )

The Arbitrary keyword argument is just like the Arbitrary argument.
(For the rest of the article I will call Arbitrary keyword argument as **kwargs, and Arbitrary argument as *args.)
If some keyword arguments don’t match the name of parameters defined in the function argument list. **kwargs will package those excessive keyword arguments as a dictionary. You can use this dictionary in the function. For instance:

# Result
pos1 = 1
pos2 = 2
kwargs = {‘kw3’: ‘three’, ‘kw4’: 4.44, ‘kw5’: {‘e’: ‘E’}}

Now, we have shown many types of arguments. If you want to use those methods simultaneously, you must care about the order you defined the argument list.

PS: Some traps you should know when you define arguments

There are some conditions and rules you should know. If you miss these conditions you might get unexpected results:

1. **kwargs should be defined in the last place of argument list. If you don’t do
this, you would get a syntax error, “invalid syntax”.

2. Optional argument should be defined after required argument, or you will
get this alert: “SyntaxError: non-default argument follows default argument”

3. If you define optional arguments or required arguments after *args, you only could change the optional arguments or required arguments by keyword.
If you define required argument after
*args, when you call the function with positional arguments only, required arguments would be blind and you would get the syntax error, “missing 1 required keyword-only argument”.

Let’s terminate this paragraph with a correct-defined function:

The meaning of * and ** before the argument which would be sent into the function

In the above article, you have learned how to use*args and **kwargs. Then you may be confused that what’s the meaning of *? Why should I code it? How does it work?

Package

If you add * before parameter listed in the argument list, such as Arbitrary argument. When you pass some positional argument into the function, Parameters in the function will package the positional arguments as a tuple.

If you add ** before parameter listed in the argument list, such as Arbitrary keyword argument. Parameters would package keyword arguments as a dictionary.
Example:

# Result
m_args = (0, 1)
m_kwargs = {‘op_arg1’: ‘op_arg1’, ‘op_arg2’: ‘op_arg2’, ‘kk’: ‘kk’}

Unpackage

If you add the * before argument(Its datatype must be a tuple)which would be passed into a function. The tuple would be unpackaged and its value would be distributed in the order of the argument list. If all of the parameters have been assigned a particular value, the rest value, which hasn’t been assigned to any parameters, would lead to a syntax error. You could use *args to receive the rest value.

If you add the ** before argument(Its datatype must be dictionary)which would be passed into a function. The dictionary would be unpackaged. If the parameter’s name defined by function could be found in the dictionary, the value matching the name of the parameter would be assigned to it.
The rest value of the dictionary, which haven’t match any parameters, would lead to a syntax error. You could use **kwargs to receive the rest values.
Example:

# Result
pos1 = p1
pos2 = p2
args = ()
kwargs = {‘pos3’: ‘p3’}
pos1 = 0
pos2 = 1
args = (2,)
kwargs = {}

The first time call major_func . We use ** to unpackage a dictionary. In the dictionary, the key 'pos1' & 'pos2' have matched parameter’s name pos1& pos2 . The parameter pos1 in major_func is assigned 'p1' .The parameter pos2 in major_func is assigned 'p2' .The rest key:value is received by **kwargs . As you can see in the result.

The second times call major_func . We use * to unpackage a tuple. In the tuple, the value 0,1,2 would be distributed in the order of the argument list in major_func. But function only can receive two arguments, the last value 3is received by *args . As you can see in the result.

Special parameter

This paragraph will introduce a special method to defined parameters. This method can let you know what type of argument should be passed into the Python function when you see the definition. These arguments include two types. One type of argument is the positional argument. The other is keyword argument. Let’s see an example of a special method.

There are two special symbols, / and * , in the list of arguments in Figure. what is the meaning of these?

Arguments before / :Arguments before / must be passed with positional-only argument when function is called. Take the figure above for example, pos1 and pos2 is positional-only argument.

Arguments between / and * : Arguments between / and * can be passed by positional or keyword. For example, in Figure, pos_or_kwd can be passed by either positional or keyword.

Arguments after * : Arguments after * can be passed with keyword-only argument when function is called. For example, in Figure, kwd1 and kwd2 should be keyword-only argument.

Both of / and * can be used independently. It also follows the rules listed above.

If you don’t use any special symbols, every argument listed in the function definition can be passed by positional or keyword.

--

--