Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Dataclasses can provide default values to attributes

from dataclasses import dataclass

@dataclass
class Point():
    x : float = 0
    y : float = 0
    name : str = 'Nameless'
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