- Non-public (aka have a single leading underscore)
- Be a parameter in the __init__ signature
Код: Выделить всё
class Foo:
def __init__(self, bar: str):
self._bar = bar
foo = Foo(bar="bar") # foo.bar would raise an AttributeError
Код: Выделить всё
from dataclasses import dataclass
@dataclass
class Foo:
bar: str # This leaves bar as a public instance attribute
Подробнее здесь: https://stackoverflow.com/questions/648 ... n-init-arg