← TOC

Chapter 3: Functions & OOP

Functions, modules, dataclass, Protocol

Functions

def greet(name: str, *, loud: bool = False) -> str:
    msg = f"Hello, {name}"
    return msg.upper() if loud else msg

dataclass

from dataclasses import dataclass

@dataclass
class User:
    id: int
    name: str
    active: bool = True

Protocol

from typing import Protocol

class Drawable(Protocol):
    def draw(self) -> None: ...

Loading comments...