I have been trying to find the answer to this question for a while now and while I am finding a lot of information on circular references and testing objects for equality, I cannot seem to find the exact answer I am looking for.
For clarity, I am working in Python 3.6.8.
Now I have different objects that I would like to create links between. They are not parents or children of each other, so I do not want to use inheritance, but I would like to link these objects through attributes. I would like to be able to have this link both ways, so I can reach the other object from either of the objects. This results into something like this:
class A():
def __init__(self, b):
self.b = b
class B():
def __init__(self, a):
self.a = a
Thus, from an instance of class A I am able to direct which instance of class B it relates to and the other way around.
However, I believe this creates some type of circular reference.
In addition, I can run into some problems when testing around these classes (unittest, pytest, etc.). In my test, I would have an object of class B which I would like to compare to the object of class B which my code returns. However, I run into recursion errors when I try to compare these objects.
In summary, the questions I have are:
- Is this type of "circular reference" a problem? Is it something I should avoid? If yes, what "better" (more pythonic) ways would there be to ensure I can still link between these objects??
- Either using the type of linking I described or another proposed method, what would be the best way in which I can test if different objects are equal? Note I do not want to see if the object ID's are equal, just if the content of the objects are equal.
Thanks in advance for everyone's time.
Aucun commentaire:
Enregistrer un commentaire