Breitburg
Breitburg

Reputation: 1

How to get parent of the class?

Here is my code:

class MyObject:
    def __init__(self):
        pass

    @property
    def parent(self) -> Test:
        return  # Return Test instance


class Test:
    def __init__(self, first: MyObject, second: MyObject):
        self.first = first
        self.second = second


Test(
    first=MyObject(),
    second=MyObject()
)

I have a Test class that takes two arguments into which objects of another class should be passed. How can I get the parent instance of the Test object from the MyObject class without passing self?

Upvotes: 0

Views: 31

Answers (1)

Lior Cohen
Lior Cohen

Reputation: 5745

Test is not the parent of the MyObject. Test contains MyObject.

Myobject can not know Test unless you pass it

Upvotes: 1

Related Questions