0
class Field:

    def __init__(self, *, read_only=False, write_only=False):
        do_something()

what's the use of specifying "*" and how it is different from using *args?

Msvstl
  • 1,116
  • 5
  • 21
  • 1
    You can read about it [here](https://docs.python.org/3/whatsnew/3.8.html#positional-only-parameters). TL;DR everything that comes after `*` should be keyword arguments only. – Ch3steR Jun 27 '22 at 06:31

2 Answers2

5

It means the constructor does not accept positional arguments but only keyword arguments.

class Field:

    def __init__(self, *, read_only=False, write_only=False):
        pass
>>> Field(True, False)
...
TypeError: __init__() takes 1 positional argument but 3 were given

>>> Field(read_only=True, write_only=False)
<__main__.Field at 0x7f3cd95fde20>
Corralien
  • 109,409
  • 8
  • 28
  • 52
2

According to the docs.

In the following example, parameters a and b are positional-only, while c or d can be positional or keyword, and e or f are required to be keywords:

def f(a, b, /, c, d, *, e, f):
    print(a, b, c, d, e, f)

So according to your code, the constructor can only take keyword arguments.

Khalil
  • 1,495
  • 6
  • 14