Код: Выделить всё
import pathlib
from typing import TypeVar, Generic
from dataclasses import dataclass, astuple
class ConfigBase:
pass
T = TypeVar("T", bound=ConfigBase)
@dataclass
class ConfigAndPath(Generic[T]):
path: pathlib.Path
config: T
Код: Выделить всё
l: list[ConfigAndPath[MyConfig]] = ...
filenames = [_path.name for _path, _my_config in l]
Код: Выделить всё
# In ConfigAndPath
def __iter__(self):
return iter(astuple(self))
Код: Выделить всё
# In ConfigAndPath
def as_tuple(self) -> Tuple[pathlib.Path, T]:
return self.path, self.config
< /code>
, который затем позволяет мне написать < /p>
filenames2 = [_path.name for _path, _my_config in [i.as_tuple() for i in l]]
Подробнее здесь: https://stackoverflow.com/questions/796 ... g-my-class