LoginSignup
9
6

More than 5 years have passed since last update.

アスタリスク'*'だけの関数のパラメータ

Last updated at Posted at 2017-03-30

Python3では、次の関数定義の例ように、*だけのパラメータというのがあります。

>>> def foo(*, x):
    print(x)

この用法の意味は、*に続くパラメータに対しては、キーワード形式での引数の渡し方を強制するということで、キーワード無しで引数を渡すと次のようにエラーになります。

>>> foo(x=1)
1
>>> foo(1)
Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    foo(1)
TypeError: foo() takes 0 positional arguments but 1 was given
9
6
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
9
6