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.
The Python "ZeroDivisionError: float division by zero" occurs when we try to divide a floating-point number by 0 . To solve the error, use an if statement to check if the number you are dividing by is not zero, or handle the error in a try/except block.
Division by zero is a logic software bug that in most cases causes a run-time error when a number is divided by zero.
Hence, if any number is divided by zero, we get the arithmetic exception .
The divide error messages happen when the computer or software attempts run a process that performs a mathematical division by zero, which is an illegal operation. This error message could also be caused by a computer or software limitation or conflict with computer memory.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With