Python - OOP tip: avoid using too many attributes on a single object


Python Clean Code Tip:

Avoid using too many attributes on a single object. Try to cluster them to improve cohesion, reduce coupling, and improve readability

👇

import datetime
from dataclasses import dataclass


# bad
@dataclass
class ExcelSheet:
    file_name: str
    file_encoding: str
    document_owner: str
    document_read_password: str
    document_write_password: str
    creation_time: datetime.datetime
    update_time: datetime.datetime


# good
@dataclass
class FileProperties:
    name: str
    encoding: str


@dataclass
class SecurityProperties:
    owner: str
    read_password: str
    write_password: str


@dataclass
class DocumentDating:
    creation_time: datetime.datetime
    update_time: datetime.datetime


@dataclass
class ExcelSheet:
    file_properties: FileProperties
    security_properties: SecurityProperties
    document_dating: DocumentDating