[Python for Hikari-] <Supplement> Chapter 06-05 Functions (arguments and return values 4)

[Python] Chapter 06-05 Arguments and return value 4

Chapter 06-04 explained the case where the argument in the function was a list.

You can also take other data structures, such as tuples and dictionaries, as arguments. This time I would like to deal with receiving tuples and dictionaries as arguments.

This section is </ font>, so if you don't have time, you can skip it. In the Fundamental Information Technology Engineer Examination, it is expected that problems such as handling tuple and dictionary arguments will be asked in the future, so let's read it.

Arbitrary number of arguments-receive as tuple-

When calling the function, the number of actual arguments and formal arguments must match as in the program below. Create a file with the file name samp06-05-01.py </ font> in chap06 </ font> and use the following code Please write.

samp06-05-01.py


def called_func(x, y, z):
    print('I'm running inside a function.')
    print(x, y, z)


#called_func(2) #← Since the number of arguments does not match, an error will occur when calling the function.
called_func(4, -1, 2)

[Execution result] </ font> I'm running inside a function. 4 -1 2

In the first function call ** called_func (2) **, the number of actual arguments is 1, but the number of formal arguments is 3, which does not match. Therefore, if you remove this comment, an error will occur.

The next call, ** called_func (4, -1, 2) **, can be called because the number of actual arguments and formal arguments match.

However, Python functions can accept any number of arguments. That is the following method. Please modify the program samp06-05-01.py </ font> that you wrote earlier and write the following code.

samp06-05-01.py


def called_func(*args):
    print('I'm running inside a function.')
    print(args)


called_func(2)    #← You can call this time
called_func(4, -1, 2)

[Execution result] </ font> I'm running inside a function. (2,) I'm running inside a function. (4, -1, 2)

If you add \ * (asterisk) to the formal argument, you can see that it is received as a tuple. The contents of ** \ * args ** are output in the function, but \ * is not required when outputting. It also uses the formal parameter name ** \ * args **, but it doesn't really matter. However, most people (customarily) use ** args **. * </ font> args is an abbreviation for argument, which means an argument.

Arbitrary number of keyword arguments ~ Receipt of dictionary ~

Chapter 06-03 dealt with keyword arguments. Actually, you can specify this keyword argument with an actual argument and receive an arbitrary number collectively with a formal argument. Create a file with the file name samp06-05-02.py </ font> in chap06 </ font> and use the following code Please write.

samp06-05-02.py


def called_func(**kwargs):
    print('I'm running inside a function.')
    print(kwargs)


called_func(x=1, y=2, z=3)

[Execution result] </ font> I'm running inside a function. {'x': 1, 'y': 2, 'z': 3}

If you add a keyword argument to the actual argument and \ * \ * (two asterisks) to the formal argument, you can see that it is received as a dictionary. The contents of ** \ * \ * kwargs ** are output in the function, but \ * \ * is not required when outputting. It also uses the formal parameter name ** \ * \ * kwargs **, but it doesn't really matter. However, most people (customarily) use ** \ * \ * kwargs **.

Finally

There are many ways to receive arguments, so let's hold them down. I don't see many cases of receiving it as a tuple or dictionary, but please be aware that you can do this as well. However, please note that there is a possibility that questions will be asked in the Information-Technology Engineers Examination.

Return to [Table of Contents Link]

Recommended Posts