Dataclasses can provide default values to attributes
examples/oop/dataclasses_default/shapes.py
from dataclasses import dataclass @dataclass class Point(): x : float = 0 y : float = 0 name : str = 'Nameless'
examples/oop/dataclasses_default/point.py
from shapes import Point p1 = Point(2, 3, 'left') print(p1) # Point(x=2, y=3, name='left') p2 = Point() print(p2) # Point(x=0, y=0, name='Nameless') p3 = Point( name = 'Good', x = 42) print(p3) # Point(x=42, y=0, name='Good')
- Attributes with default values must before attributes without default