*args・**kwargs

(*args)=可変長位置引数を取る関数を定義

慣習的にargsが用いられるが何でもよい

与えられた実引数はタプルとなる

[code lang=”python” title=””]
def arg_test(*args):
for a in args:
print(a)

apple = ‘one’
orange = ‘two’

arg_test(apple,orange)

#実行結果
#one
#two
[/code]

(**kwargs)=可変長キーワード引数を取る関数を定義

慣習的にkwargsが用いられるが何でもよい

与えられた実引数は辞書となる

[code lang=”python” title=””]
def kwargs_test(**kwargs):
print(kwargs)

kwargs_test(apple=’one’,orange=’two’)

#実行結果
#{‘apple’: ‘one’, ‘orange’: ‘two’}
[/code]

python

Posted by iser