0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

"可変長引数"の意味がやっとわかった初心者

Last updated at Posted at 2021-09-28

まずは普通のパターン。

def arg_loop(pro):
     for athlete in pro:
             print(athlete)
 
arg_loop('堀口','那須川','Dustin Pague','Floyd Mayweather')

#結果
TypeError: arg_loop() takes 1 positional argument but 4 were given

普通は関数は1つしか取れないので4つも与えても対応してくれません。そこで便利なのが可変長引数です。4つでも5つでも与えた分だけ(=可変長)、下のように結果を返してくれます。引数proの前に「*」をつけるだけです。

def arg_loop(*pro):
     for athlete in pro:
             print(athlete)
 
arg_loop('堀口','那須川','Dustin Pague','Floyd Mayweather')

#結果
堀口
那須川
Dustin Pague
Floyd Mayweather

参考文献:https://dev.classmethod.jp/articles/what-does-asterisk-mean-at-args/

0
0
4

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?