Function parameters with only an asterisk'*'

In Python3, there are only * parameters, as in the following function definition example.

python


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

The meaning of this usage is to force the argument to be passed in keyword format for the parameter following *, and if you pass the argument without the keyword, the following error will occur.


>>> 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

Recommended Posts

Function parameters with only an asterisk'*'
Creating an authentication function
Note: The meaning of specifying only * (asterisk) as an argument in the Python function definition.
Kernel regression with Numpy only
Try function optimization with Optuna
Implement login function with django-allauth
Create an environment with virtualenv
Create an API with Django
Approximate sin function with TensorFlow
Try an autoencoder with Pytorch
Creating an egg with python
Tuning Keras parameters with Keras Tuner
Adjusting LightGBM parameters with Optuna
4. Circle parameters with neural network!
Zura with softmax function implemented
[Python] I made an image viewer with a simple sorting function.
[Python] Explains how to use the format function with an example
I tried to make an image similarity function with Python + OpenCV