Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
python3
dataclass
communitycreator

What are data classes in Python?

abhilash

Python is an object-oriented programming language. So, everything in Python is an object. However, writing classes in Python sometimes means you may write a lot of boilerplate code to create a class instance from its inputs.

Therefore, data classes were introduced in Python 3.7 to make writing classes less verbose.

dataclass.py
normal-class.py
from dataclasses import dataclass

@dataclass
class Student:
    name: str
    age: int
    school: str

s = Student("abhi", 23, "high school")
print(s)

When you specify the properties or fields of the class, the dataclass will generate the constructor to intialize the values of the fields. dataclass will also generate code for several dunder methods of a class.

field function in dataclass

The field function is used to control class attributes in the dataclass, such as provide default values, and include or exclude any particular attribute in the repr method, etc.

The function signature is as follows:

field(
    *,
    default,
    default_factory,
    init=True,
    repr=True,
    hash=None,
    compare=True,
    metadata=None,
)
Parameter Description
default default is the default value of the field
default_factory default_factory is a 0-argument function called to initialize a field’s value
init If True, the field will be a parameter to the class’s init() function
repr If True, the field will be included in the object’s repr()
hash If True, the field will be included in the object’s hash()
compare If True, the field will be used while comparing objects of the class
metadata It’s a mapping that is stored as metadata for the field

You cannot use default and default_factory to initialize a field.

To view all the object’s fields, use the function fields() on the object.

fields-function.py
field-default-factory.py
from dataclasses import dataclass, field, fields

@dataclass
class Student:
    name: str = field(default="abhi", metadata={"description":"student name"})
    age: int = 23
    school: str = "harvard"

s = Student()

print(fields(s))

Immutable dataclasses

A dataclass can be made immutable, which means that the fields of the class are closed for modification. If we try to modify the fields, an exception will be thrown.

Immutable data classes are thread-safe since their content cannot be changed.

Another interesting aspect is that the frozen classes can’t modify themselves either.

Refer to the example below on how to make a dataclass immutable.

frozen-dataclass.py
method-modify.py
from dataclasses import dataclass

@dataclass(frozen=True)
class Student:
    name: str = "abhi"
    age: int = 23
    school: str = "harvard"

    def change_name(self, new_name):
        self.name = new_name


s = Student()
s.change_name("john")

RELATED TAGS

python
python3
dataclass
communitycreator
RELATED COURSES

View all Courses

Keep Exploring