avatarSunil Kumar

Summary

The provided web content explains how to create Python dataclasses with keyword-only fields using the dataclasses module, which is supported from Python 3.10 onwards.

Abstract

The article titled "Python Tips: How to Create Dataclass with Keyword-only fields??" discusses the enhancement in Python's dataclasses module that allows for the creation of dataclasses with keyword-only fields in the __init__ method. It begins by illustrating a basic dataclass without keyword-only fields and then demonstrates how to define a dataclass where all fields are keyword-only by setting the kw_only=True parameter in the @dataclass decorator. The article further explains that keyword-only fields can be specified on a per-field basis using the Field function from the dataclasses module, or by using the KW_ONLY marker to indicate that all subsequent fields are keyword-only. The article emphasizes that attempting to instantiate a dataclass with keyword-only fields without using keywords will result in an exception. It concludes by providing a reference link to the official Python documentation for further reading on keyword-only fields.

Opinions

  • The author conveys that defining keyword-only fields in dataclasses can help prevent positional argument errors and improve code clarity.
  • The use of the kw_only=True parameter is presented as a straightforward way to make all fields in a dataclass keyword-only.
  • The article suggests that specifying keyword-only fields on a per-field basis offers flexibility when some fields should be mandatory while others are optional.
  • The inclusion of the KW_ONLY marker as a way to denote all following fields as keyword-only is highlighted as a significant feature for developers to maintain the order of fields in a dataclass while ensuring newer fields are keyword-only.
  • The author's choice to include code examples and screenshots of exception messages demonstrates a pedagogical approach, aiming to provide clear and practical guidance for Python developers.

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

Python3
Python Dataclass
Python Programming
Python
Recommended from ReadMedium