Python Tips: How to Create Dataclass with Keyword-only fields ??

Python dataclasses
now support fields that are keyword-only in the generated __init__ method. There are several ways of specifying keyword-only fields.
let's see with an example.
from dataclasses import dataclass
@dataclass
class Car:
vin_number:str
model:str
color:str
car= Car('1235','rav4','red')
print(car)
output:
Car(vin_number='1235', model='rav4', color='red')
Now let's see how we can create Keyword-only fields for data class
from dataclasses import dataclass
@dataclass(kw_only=True)
class Car:
vin_number:str
model:str
color:str
now when we try to create an instance of dataclass without specifying keyword args, we will get an exception because it is expecting the keyword args.

so when need to pass keyword args like below.

You can specify keyword-only on a per-field basis:

You can also specify that all fields following a KW_ONLY marker are keyword-only. This will probably be the most common usage:

Reference:
https://docs.python.org/3/whatsnew/3.10.html#keyword-only-fields