Python has methods to pass a number of arguments to a function.
Let's figure it out.
Non-Keyword Arguments
It is not compulsory, but it is mainly used in the form *args.
This is the basic usage.
def testArgs(*args):
for arg in args:
print(arg)
testArgs('hello', 'world', 7, 3.14)
# hello
# world
# 7
# 3.14
You can pass a number of arguments with this method.
Let's test with the normal arguments.
def testArgs(arg1, arg2, *args):
for arg in args:
print(arg)
print('=======')
print(f'{arg1}, {arg2}')
testArgs('hello', 'world', 7, 3.14)
# 7
# 3.14
# =======
# hello, world
After the passed arguments are assigned in order, all remaining arguments are assigned to non-keyword arguments.
You have to put the non-keyword arguments to the last.
You can put the non-keyword arguments to the first location, but you need to pass arguments with the name of arguments.
def testArgs(*args, arg1, arg2):
for arg in args:
print(arg)
print('=======')
print(f'{arg1}, {arg2}')
testArgs('hello', 'world', arg1=7, arg2=3.14)
# hello
# world
# =======
# 7, 3.14
You can't put the non-keyword arguments to the other locations.
Keyword Arguments
Keyword arguments is similar to non-keyword arguments except that it can have key, value pair.
def testKwargs(**kwargs):
for key, value in kwargs.items():
print (f'{key}, {value}')
testKwargs(key1='hello', key2=7, key3=3.14)
# key1, hello
# key2, 7
# key3, 3.14
Now, let's test with the normal arguments.
def testKwargs(arg1, arg2, **kwargs):
print(f'{arg1}{arg2}')
for key, value in kwargs.items():
print (f'{key}, {value}')
testKwargs('python', 3, key1='hello', key2=7, key3=3.14)
# python3
# key1, hello
# key2, 7
# key3, 3.14
testKwargs(arg2='python', arg1=3, key1='hello', key2=7, key3=3.14)
# 3python
# key1, hello
# key2, 7
# key3, 3.14
It is the same as non-keyword arguments.
Both Non-Keyword Arguments and Keyword Arguments
We can even use both non-keyword arguments and keyword arguments simultaneously.
def testFunc(*args, **kwargs):
print(f'args: {args}')
print(f'kwargs: {kwargs}')
testFunc('hello', 'world', 3, one=1, two='second',three=3.14)
# args: ('hello', 'world', 3)
# kwargs: {'one': 1, 'two': 'second', 'three': 3.14}
It is distinguished by the name of arguments.
And it can use with normal arguments.
Let's go over one last thing.
argsTuple = (1, 2, 3)
argsList = ['one', 'two', 'three']
kwargs = {'arg1': 1, 'arg2': 2, 'arg3': 3}
def testArgs(arg1, arg2, arg3):
print(f'arg1: {arg1}, arg2: {arg2}, arg3: {arg3}')
def testKwargs(arg1, arg2, arg3):
print(f'arg1: {arg1}, arg2: {arg2}, arg3: {arg3}')
testArgs(*argsTuple)
# arg1: 1, arg2: 2, arg3: 3
testArgs(*argsList)
# arg1: one, arg2: two, arg3: three
testKwargs(**kwargs)
# arg1: 1, arg2: 2, arg3: 3
When using this, the same argument name as the key in the dictionary must be used.
You can leave now, please use this in your code.
'Python' 카테고리의 다른 글
[Python] Introduce to DearPyGui (1) | 2021.02.11 |
---|---|
[Python] argparse module (0) | 2021.02.03 |
[Python] 3 Ways of Formatted String (0) | 2021.01.22 |
[Python] List (Array) Data Structure (0) | 2020.12.07 |
[Python] Dictionary Data Structure (0) | 2020.12.06 |
댓글