LoginSignup
0
0

More than 1 year has passed since last update.

Python_*args, **kwarge(可変長引数)

Last updated at Posted at 2021-11-24

*args

いくつでもの引数をタプルとして受け取る
*argsはargsではなくどんな文字列でもよい。

def challenge_args(*args):
    print(args)
    print(args[0])
    print(sum(args))


challenge_args(1, 2, 3, 4)
実行結果
(1, 2, 3, 4)
1
10
def challenge_apple(*apple):
    print(apple)
    print(sum(apple))


challenge_apple(1, 2, 3)
実行結果
(1, 2, 3)
6

**kwargs

いくつかのキーワード引数を辞書として受け取る。

def challenge_kwarges(**kwargs):
    print(kwargs)
    apple = kwargs.get('apple', None)
    print(apple)


challenge_kwarges(apple='red', banana='yellow')
実行結果
{'apple': 'red', 'banana': 'yellow'}
red
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0