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

Declaring attributes (dataclasses)

  • Starting from 3.7 dataclasses
  • Typehints are required but not enforced!
from dataclasses import dataclass

@dataclass
class Point():
    x : float
    y : float
    name : str

from shapes import Point

p1 = Point(2, 3, 'left')
print(p1.x)    # 2
print(p1.y)    # 3
print(p1.name) # left

p1.x = 7       # 7
print(p1.x)

p1.color = 'blue'
print(p1.color)   # blue

p1.x = 'infinity' # infinity
print(p1.x)