So, I have these two dataclasses in a file:
@dataclass
class A:
children: List[B]
@dataclass
class B:
parent: A
, which are possible with the use of the __future__.annotations
feature.
Then I have two other files, each with a bunch of objects for each type that are static for my project.
File objects_A
:
import objects_B
obj_a1 = A(
children=[
objects_B.obj_b1,
objects_B.obj_b2
]
)
File objects_B
:
import objects_A
obj_b1 = B(
parent=objects_A.obj_a1
)
obj_b2 = B(
parent=objects_A.obj_a1
)
Obviously, there a circular dependency problem between the files, but it wouldn't work even if they were in the same file, as a variable of one type depends on the other to succeed.
Initialising the B
objects inside obj_a1
also won't work as there is no concept of self
here.
At the moment, I'm setting parent
to None
(against the type hinting), and then do a loop on obj_a1
to set them up:
for obj_b in obj_a1.children:
obj_b.parent = obj_a1
Any bright ideas folks?
Don't know if it helps, but these objects are static (they will not change after these declarations) and they have kind of a parent-children relationship (as you surely have noticed).
If possible, I would like to have the variables of each type in different files.
dict = {PARENT_OBJ: [CHILDREN]}
– HayseB
around the project and be able to access other stuff from parentA
without needing to use other structures, like dictionaries. – ProhibitiveA
s with an empty list for children, and only add theB
s to the relationship when their object file is executed. Do none of these two options work? – Receiver