I have this question regarding the advantage and disadvantage of @property
in a Python Object. This morning i have read several posts and forum but i still have not a clear idea. I create to simple object in Python to understand the differences of a Object without and with @property
. My questions are: when i need to use @property
to create an object? is useful for simple object like the example? when i don't need to use @property
for an Object?
class MyArithmetic(object):
def __init__(self, a, b):
self.a = a
self.b = b
def sum(self):
return self.a + self.b
def subtraction(self):
return self.a - self.b
def multiplication(self):
return self.a * self.b
def division(self):
return self.a/self.b
test = MyArithmetic(10,5)
print test.sum()
print test.subtraction()
print test.multiplication()
print test.division()
class MyArithmetic2(object):
def __init__(self, a, b):
self.a = a
self.b = b
@property
def sum(self):
return self.a + self.b
@property
def subtraction(self):
return self.a - self.b
@property
def multiplication(self):
return self.a * self.b
@property
def division(self):
return self.a/self.b
test2 = MyArithmetic2(10,5)
print test2.sum
print test2.subtraction
print test2.multiplication
print test2.division