KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Let's consider python (3.x) scripts: main.py: from test.team import team from test.user import user if __name__ == '__main__': u = user() t = team() u.setTeam(t) t.setLeader(u) test/user.py: from test.team import team class user: def setTeam(self, t): if issubclass(t, team.__class__): self.team = t test/team.py: from test.user import user class team: def setLeader(self, u): if issubclass(u, user.__class__): self.leader = u Now, of course, i've got circular import and splendid ImportError. So, not being pythonista, I have three questions. First of all: i. How can I make this thing work ? And, knowing that someone will inevitably say "Circular imports always indicate a design problem", the second question comes: ii. Why is this design bad? And the finally, third one: iii. What would be better alternative? To be precise, type checking as above is only an example, there is also a index layer based on class, which permits ie. find all users being members of one team (user class has many subclasses, so index is doubled, for users in general and for each specific subclass) or all teams having given user as a member Edit: I hope that more detailed example will clarify what i try to achieve. Files omitted for readibility (but having one 300kb source file scares me somehow, so please assume that every class is in different file) # ENTITY class Entity: _id = None _defs = {} _data = None def __init__(self, **kwargs): self._id = uuid.uuid4() # for example. or randint(). or x+1. self._data = {}.update(kwargs) def __settattr__(self, name, value): if name in self._defs: if issubclass(value.__class__, self._defs[name]): s
Tags (comma-separated)
Save Edits
Cancel