ID EN
Typing

ClassVar

Python 3.11

Special type construct to mark class variables.

Syntax

PYTHON
typing.ClassVar

Examples

Example 1
PYTHON
class Starship:
    stats: ClassVar[dict[str, int]] = {}  class variable
    damage: int = 10                      instance variable
Example 2
PYTHON
enterprise_d = Starship(3000)
enterprise_d.stats = {}  Error, setting class variable on instance
Starship.stats = {}      This is OK

See Also

ClassVar ClassVar isinstance() issubclass() ClassVar