1

I want to do something like this:

class B():
    def __init__(self,c):
        self.c=c
    def getA(self):
        #this is what I want
class A():
    def __init__(self,d):
        self.b = B(d)

a = A(d)

what I want is that when I perform

a.b.getA()

I can get the object a that use this method by its attribute b. I can't pass any new variable to the function because this in implementation is a class written by other people that changing the input variables means changing a lot of stuff.

Can anyone help?

5
  • 2
    but you already have a, don't you? Commented Nov 9, 2015 at 20:43
  • seems that way to me... a.b.getA() Commented Nov 9, 2015 at 20:44
  • @sobolevn I can only modify the method getA(), so I want to get the object using this method. Commented Nov 9, 2015 at 20:47
  • 1
    No, you can't do that. References have only one direction. Objects never know what other objects (note: plural!) may reference them. The exception is when you explicitly store a reference in the object (eg modifying class B so it stores a reference to A, which must then be initialized) Commented Nov 9, 2015 at 20:54
  • I am not the one using the object a. In other word, I need a function in b that can get the object a. Commented Nov 9, 2015 at 20:54

1 Answer 1

1

Since it seems that you construct the B instance within the A constructor, then give your B instance a reference to the A instance:

class B():
    def __init__(self, c, a):
        self.c = c
        self.a = a
    def getA(self):
        return self.a

class A():
    def __init__(self, d):
        self.b = B(d, self)

a = A(d)

It is possible under certain Python implementations to sometimes find an instance using the gc.get_referrers, but as the documentation says, using it should be avoided "for any purpose other than debugging.":

import gc

class B():
    def __init__(self,c):
        self.c = c

    def getA(self):
        for i in gc.get_referrers(self):
            if not isinstance(i, dict):
                continue

            for j in gc.get_referrers(i):
                if isinstance(j, A) and getattr(j, 'b', None) is self:
                    return j

        return

class A():
    def __init__(self,d):
        self.b = B(d)

a = A(42)
print(a.b.getA() is a)

The first get_referrers call will find among others, the __dict__ of the A instance; this would probably be of type dict; we then go through all the objects of the dictionaries referring to self, and if any of these is of type A, and it has attribute b whose value is self, we return that object.

Thus the clause print(a.b.getA() is a) shall print true.

But seriously, don't use this code.

Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way I don't need to change the initial method of the classes, I'm trying to keep modifications in a single method to minimize the modification of the code. Changing initialization will change a lot of things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.