On this page
typing.ClassVar
Prev: typing.ChainMap | Up: typing | Next: typing.clear_overloads
Rel: typing.AbstractSet, typing.Annotated, typing.Any, typing.assert_never
Ref: docs.python.org — typing.ClassVar
Tags: #public #python3 #stdlib #typing
Import
import typing
from typing import ClassVar
Signature & Overview
typing.ClassVar(*args, **kwds)
Special type construct to mark class variables.
Usage
from dataclasses import dataclass
from typing import ClassVar
@dataclass
class User:
name: str
species: ClassVar[str] = "human"
u = User("Ada")
assert u.species == "human" and "species" not in u.__dict__
Gotchas
- Tells
@dataclassto ignore the attribute as an instance field: Also tells type checkers not to assign it throughself.species = ....
REPL Inspect
from typing import ClassVar
help(ClassVar)
Linked from
Related notes
- tuts/python3/stdlib_module/typing/ChainMap Linked here; links to this page; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/clear_overloads Linked here; links to this page; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/typing/AbstractSet Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/Annotated Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/Any Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/assert_never Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/assert_type shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/AsyncContextManager shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/AsyncGenerator shared tags: #stdlib, #typing