Как разрешить циклическую зависимость (Python) ⇐ Python

Программы на Python
Anonymous
Как разрешить циклическую зависимость (Python)

Сообщение Anonymous »

Я реализую виртуальную файловую систему
столкнулся с проблемой циклического импорта:

Код: Выделить всё

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from .directory import Directory
from .archive import Archive

class VFile:
def __init__(self, data: bytes, parent: Directory | Archive ...):
...

Код: Выделить всё

class Archive(VFile):
def _read(self):
for m in metadata:
data = ...
FileType = guess_type(data)
self.files.append(FileType(data, self))
...

Код: Выделить всё

from .util import guess_type
class Directory(VFile):
def _parse_btree(self):
FileType = guess_type(data)
self.files.append(FileType(data, self))
...

Код: Выделить всё

from .common import VFile
from .archive import Archive
from .directory import Directory

def guess_type(data: bytes) -> type[VFile]:
if is_archive(data):
return Archive
if is_dir(data):
return Directory
...
return VFile
использование TYPE_CHECKING решает часть проблемы, но я не могу решить остальную часть, не объединив все файлы в один (очень большой файл)
Как это реорганизовать?


Подробнее здесь: https://stackoverflow.com/questions/798 ... ncy-python

Вернуться в «Python»