I have a Class in python, with the following attributes:
self.number1 = 0 self.number2 = 0 self.divided = self.number1/self.number2
This of course throws up the zero error:
ZeroDivisionError: integer division or modulo by zero
The idea is that I will increment number1 and number2 later on, but will self.divided be automatically updated? If it is auto updated then how do I get around the zero error? Thanks.
No, self.divided
is a simple attribute and will not automatically update. For dynamic attributes, use a property
instead:
class Foo(object):
number1 = 0
number2 = 0
@property
def divided(self):
return self.number1 / self.number2