Python - how to get class instance reference from an attribute class?

class A()    
    att = B()    

class B()    
    ...

a = A()

b = B()

a.att = b

How can b get reference of a ? I need to get an attribute of a here.

Thanks!

Answers


You can make a generic "Reference()" class, that keep any reference of itself in an attributes dictionnary.

class Reference(object):
    def __init__(self):
        self.references = {}

    def __setattr__(self, key, value):
        if hasattr(self, 'references'):
            if isinstance(value, Reference):
                if not key in value.references:
                    value.references[key] = []
                value.references[key].append(self)

            elif value is None and hasattr(self, key):
                old = getattr(self, key).references
                if key in old and self in old[key]:
                    old[key].remove(self)

        super(Reference, self).__setattr__(key, value)

And then, create your classes :

class A(Reference):
    def __init__(self):
        super(A, self).__init__()
        self.att = None

class B(Reference):
    def __init__(self):
        super(B, self).__init__()
        self.att = None

And use it :

a = A()
b = B()

print 'A references', a.references
print 'B references', b.references
# A references {}
# B references {}

a.att = b

print 'A references', a.references
print 'B references', b.references
# A references {}
# B references {'att': [<__main__.A object at 0x7f731c8fc910>]}

At the end, you'll have back reference to all Reference class from any properties


Easiest way would be to just add an extra function parameter to the method in B that needs A, and pass it through when called. Or, just make B's init take an A as argument, and change the bit in A's init to be att = B(self)


class A(object):
    def __init__(self):
        self.att = B(self)

class B(object):
    def __init__(self, a):
        self.a = a

a = A()

a.att.a is a

Or another way,

class A(object):
    def __init__(self, b):
        b.a = self
        self.att = b

class B(object):
    pass

a = A(B())

a.att.a is a

This code doesn't make a lot of sense... but if I correctly understand your question...

class A(object):
    pass     #or whatever you like

class B(object):
    def __init__(self, ref):  #accept one argument
        self.ref = ref

a = A()

b = B(a) #pass `a` as that argument

a.att = b

Might be one answer.


class A(object):

    def __init__(self):
        self._att=None

    @property
    def att(self):
        return self._att

    @att.setter
    def att(self, value):
        self._att = value
        value.parent = self


class B(object):

    pass

a = A()

b = B()

a.att = b
print b.parent

Need Your Help

Setting onChange event for GPIO interface in Node.JS

node.js events raspberry-pi gpio

I have Node.JS installed on my Raspberry Pi with pi-gpio installed giving me access to the gpio ports. I can read and write ok, but given the event driven nature of node.js, is it possible to set i...

Nodejs v0.10.x (freebsd) “X509_STORE_add_cert:cert already in hash table”

node.js ssl ssl-certificate freebsd

I'm work with async web api and have a problem in nodejs version higer than v0.8.9