class A:
    x=1
    def __add__(self, obj):
        if isinstance(obj,A):
            return self.x+obj.x
        return "False"
class B(A):
    x=2
a=A()
b=B()
print(a+b)
            Asked
            
        
        
            Active
            
        
            Viewed 25 times
        
    -1
            
            
         
    
    
        John Anderson
        
- 35,991
- 4
- 13
- 36
 
    
    
        Vedant Bhosale
        
- 1
- 3
- 
                    2In the `__add__()` method, the `obj` argument is the `b` instance of class `B`. – John Anderson Feb 08 '19 at 03:43
- 
                    but while executing a=A() what gets passed into obj arguments? – Vedant Bhosale Feb 08 '19 at 03:46
- 
                    1The `__add__` method is not called in `a = A()`. – John Anderson Feb 08 '19 at 03:48
- 
                    `The __add__ method is not called in a = A()` - so nothing is passed to the `obj` parameter. – wwii Feb 08 '19 at 04:31
- 
                    It isn't clear what your question is. https://docs.python.org/3/library/functions.html#isinstance – wwii Feb 08 '19 at 04:41
1 Answers
0
            
            
        The add method takes self, the first object in the addition, and another one, other.
For example:
class A:
    def __init__(self, x):
        self.x=x
    def __add__(self, obj):
        if isinstance(obj,A):
            return self.x+obj.x
        raise NotImplementedError
a = A(3)
b = A(4)
res = a + b  # this is essentially a.__add__(obj=b) 
             # self is implicitly the object a
# res == 7
 
    
    
        Charles
        
- 3,116
- 2
- 11
- 20
- 
                    how does if isinstance condition gets satisfied as obj is instance of Class B? – Vedant Bhosale Feb 08 '19 at 04:10
- 
                    2Well since B is a subclass of A, `isinstance` will still return `True`. Take a look at [this great SO post](https://stackoverflow.com/questions/1549801/what-are-the-differences-between-type-and-isinstance) – Charles Feb 08 '19 at 04:15