Skip to content

Dataclasses

Dataclasses are a new feature in Python 3.7. They are a convenient way to create classes which are mainly used to store data. By default, dataclasses provide a repr and init method, so we don't have to write them ourselves.

src.beginner.dataclasses

Module to create a dataclass Circle, with properties and methods.

This module contains a class to explain dataclass, properties and methods with args and *kwargs.

src.beginner.dataclasses.Circle dataclass

Circle Class.

Source code in src/beginner/dataclasses/dataclasses.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
@dataclass
class Circle:
    """Circle Class."""

    radius: float
    decimal_precision: int

    @property
    def diameter(self) -> float:
        """Return the diameter of the circle.

        Returns:
            Diameter of the circle
        """
        diameter = 2 * self.radius
        return round(diameter, self.decimal_precision)

    @property
    def area(self) -> float:
        """Return the area of the circle.

        Returns:
            Area of the circle
        """
        area: float = (self.radius**2) * pi
        return round(area, self.decimal_precision)

    @property
    def perimeter(self) -> float:
        """Return the perimeter of the circle.

        Returns:
            Perimeter of the circle
        """
        perimeter: float = 2 * self.radius * pi
        return round(perimeter, self.decimal_precision)

    @classmethod
    def set_circle_args(cls, *args) -> "Circle":
        """Set a Circle instance with positional arguments.

        Other Parameters:
            r (float): Positional argument to create a Circle class,
                represents the radius
            d (int): Positional argument to create a Circle class,
                represents the decimal precision
        Returns:
            A circle instance
        """
        circle: Circle = cls(*args)
        return circle

    @classmethod
    def set_circle_kwargs(cls, **kwargs) -> "Circle":
        """Set a Circle instance with keyword arguments.

        Other Parameters:
            radius (float): Keyword argument to create a Circle class,
                represents the radius
            decimal_precision (int): Keyword argument to create a Circle class,
                represents the decimal precision
        Returns:
            A circle instance
        """
        circle_kwargs: Circle = cls(**kwargs)
        return circle_kwargs
area: float property

Return the area of the circle.

Returns:

Type Description
float

Area of the circle

diameter: float property

Return the diameter of the circle.

Returns:

Type Description
float

Diameter of the circle

perimeter: float property

Return the perimeter of the circle.

Returns:

Type Description
float

Perimeter of the circle

set_circle_args(*args) classmethod

Set a Circle instance with positional arguments.

Other Parameters:

Name Type Description
r float

Positional argument to create a Circle class, represents the radius

d int

Positional argument to create a Circle class, represents the decimal precision

Returns: A circle instance

Source code in src/beginner/dataclasses/dataclasses.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
@classmethod
def set_circle_args(cls, *args) -> "Circle":
    """Set a Circle instance with positional arguments.

    Other Parameters:
        r (float): Positional argument to create a Circle class,
            represents the radius
        d (int): Positional argument to create a Circle class,
            represents the decimal precision
    Returns:
        A circle instance
    """
    circle: Circle = cls(*args)
    return circle
set_circle_kwargs(**kwargs) classmethod

Set a Circle instance with keyword arguments.

Other Parameters:

Name Type Description
radius float

Keyword argument to create a Circle class, represents the radius

decimal_precision int

Keyword argument to create a Circle class, represents the decimal precision

Returns: A circle instance

Source code in src/beginner/dataclasses/dataclasses.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
@classmethod
def set_circle_kwargs(cls, **kwargs) -> "Circle":
    """Set a Circle instance with keyword arguments.

    Other Parameters:
        radius (float): Keyword argument to create a Circle class,
            represents the radius
        decimal_precision (int): Keyword argument to create a Circle class,
            represents the decimal precision
    Returns:
        A circle instance
    """
    circle_kwargs: Circle = cls(**kwargs)
    return circle_kwargs

Properties

Dataclasses can have properties, which are computed attributes. They are defined by using the @property decorator. And they can be used like normal attributes, without parentheses.

*args and **kwargs

Methods can be called with *args and **kwargs. *args represents a tuple of positional arguments, and **kwargs represents a dict of keyword arguments. This is useful when we want to pass a variable number of arguments to a method, or when we want to capture arguments that we don't know about.

References