Let's go through the call to a.method2.
- You call
a.method(). This returns a new instance of A
- This new instance is passed to
a.method2 as the local variable f
- You then attempt to call
a.f (through self), but f is an instance
You aren't calling self.<some_function>, you are trying to say that an instance of A has an attribute that is also an instance of A, which is not the case. It's effectively doing:
a.A(a.x+1, a.y+1).x
Which raises your error. Yes, an instance of A has the attribute x, but no instance of A has an attribute matching this new instance. It's not totally clear what you are trying to accomplish. If you are trying to test the value of the attribute on a new instance, you might just do:
class A:
def __init__(self, x
self.y = y
def method(self):
return A(self.x + 1, self.y + 1)
def method2(self):
# just check the value itself
if self.x > 3:
return True
else:
return False
a = A(1, 2)
# b is a new instance, you shouldn't be testing values of b
# via some function in a, because really this function
# exists in all instances, including b
b = a.method()
# so check b.x through the instance b instead
b.method2()
False