Python: accessing attributes and methods of one class in another

Let's say I have two classes A and B:

Class A: # A attributes and methods here Class B: # B attributes and methods here 

Now I can evaluate the properties of A in an object of class B as follows:

 a_obj = A() b_obj = B(a_obj) 

I need two way access. How to access properties A in properties B and B in A?

+8
python oop
source share
3 answers

You need to create pointers anyway:

 class A(object): parent = None class B(object): def __init__(self, child): self.child = child child.parent = self 

Now A can refer to self.parent (provided that it is not None ), and B can refer to self.child . If you try to make an instance A child of more than one B , the last "parent" wins.

+10
source share

Why not just plan your objects so that you can take care of inheritance.

 class A(object): # stuff class B(A): # has A methods/properties class C(B): # has A and B methods/properties 

In this case, planning ahead, you can simply use C for the universal object and A with B as more specialized / naked parents.

+5
source share

This method will be very useful since you can use objects of both classes interchangeably. This is a serious problem, I will explain that in the end.

 class A: def MethodA(self): return "Inside MethodA" def __init__ (self, Friend=None): self.__dict__['a'] = "I am a" self.__dict__['Friend'] = Friend if Friend is not None: self.__dict__['Friend'].__dict__['Friend'] = self def __getattr__(self, name): if self.Friend is not None: return getattr(self.Friend, name) raise AttributeError ("Unknown Attribute `" + name + "`") def __setattr__(self, name, value): if self.Friend is not None: setattr(self.Friend, name, value) raise AttributeError ("Unknown Attribute `" + name + "`") class B: def MethodB(self): return "Inside MethodB" def __init__ (self, Friend=None): self.__dict__['b'] = "I am b" self.__dict__['Friend'] = Friend if Friend is not None: self.__dict__['Friend'].__dict__['Friend'] = self def __getattr__(self, name): if self.Friend is not None: return getattr(self.Friend, name) raise AttributeError ("Unknown Attribute `" + name + "`") def __setattr__(self, name, value): if self.Friend is not None: setattr(self.Friend, name, value) raise AttributeError ("Unknown Attribute `" + name + "`") 

Explanation:

According to this page , __getattr__ and __setattr__ will be called on python objects only when the requested attribute is not found in the specific object space. Therefore, in the constructor, we establish a connection between both classes. And then when __getattr__ or __setattr__ , we reference another object using the getattr method. ( getattr , setattr ) We use __dict__ to assign values โ€‹โ€‹in the constructor so that we don't call __setattr__ or __getattr__ .

Run Examples:

 b = B() # print ba # Throws AttributeError, as A and B are not related yet a = A(b) print aa print ab print ba # Works fine here, as 'a' is not found b, returns A a print bb print a.MethodA() print a.MethodB() print b.MethodA() print b.MethodB() I am a I am b I am a I am b Inside MethodA Inside MethodB Inside MethodA Inside MethodB 

Now, a serious problem:

If we try to access an attribute that is not in both of these objects, we end up with infinite recursion. Suppose I want to access 'C' from 'a'. Since C is not in a, it calls __getattr__ and will refer to the b object. Since object b does not have C, it calls __getattr__ , which will reference object a. Thus, we find ourselves in infinite recursion. Thus, this approach works fine when you are not accessing something unknown for both objects.

+3
source share

All Articles