I tried properties over getter and setters as in below code, when class B test function is called I expected class A setter will be called but unfortunately a new instance variable of class B is created. This behavior is observed in python 2.7.13 and it works fine in python 3.
Code:
class A:
def __init__(self):
self.a = 10
@property
def vala(self):
print ("Into Vala getter")
return self.a
@vala.setter
def vala(self, a):
print ("Into Vala setter")
self.a = a
class B:
def test(self):
self.a = A()
self.a.vala = 10
print ("B.test completed")
b = B()
b.test()
Output with python 2.7.13
B.test completed
Output with python 3
Into Vala setter
B.test completed
My question is if this is the expected behavior how to work with composition in python 2.7?
This is a duplicate of Python property decorator not working, why? In Python 2, you need to inherit from object to get the same behavior as Python 3, otherwise you are still in old-style classes land.