UML (Unified Modeling Language) is a picture language for describing a design. A diagram is not an archive document but a tool for conversation: it conveys a design idea to the team quickly and helps spot a mistake before any code is written. The two most useful kinds are the class diagram (structure) and the sequence diagram (behaviour).

The class box

A class is a rectangle with three parts: the name, the attributes and the methods. Every member carries a visibility mark.

UMLMeaningIn Python
+publicname
#protected_name
-private__name

The code below builds such a description from a Python class: attributes come from the annotations, methods through inspect.

python
import inspect

class Account:
    owner: str
    _balance: float

    def __init__(self, owner):
        self.owner = owner
        self._balance = 0.0

    def deposit(self, amount):
        self._balance += amount

    def _audit(self):
        pass

def sign(name):
    return "-" if name.startswith("__") else "#" if name.startswith("_") else "+"

def to_uml(cls):
    lines = [cls.__name__]
    for name, tp in cls.__annotations__.items():
        lines.append(f"  {sign(name)} {name}: {tp.__name__}")
    for name, fn in inspect.getmembers(cls, inspect.isfunction):
        if not (name.startswith("__") and name.endswith("__")):
            params = ", ".join(list(inspect.signature(fn).parameters)[1:])
            lines.append(f"  {sign(name)} {name}({params})")
    return "\n".join(lines)

print(to_uml(Account))
text
Account
  + owner: str
  # _balance: float
  # _audit()
  + deposit(amount)