class Fraction(object):
def __init__(self, numerator, denominator):
"""Inits Fraction with values numerator and denominator."""
self.__numerator = numerator
if(denominator==0):
raise ValueError('Zero as denominator')
else:
self.__denominator = denominator
def getNumerator (self):
"""*Returns the numerator of a Fraction."""
return self.__numerator
def getDenominator (self):
"""Returns the denominator of a Fraction."""
return self.__denominator
def setNumerator (self, value):
"""Sets the numerator of a Fraction to the provided value.”"""
self.__numerator = value
def setDenominator (self, value):
"""Sets the denominator of a Fraction to the provided value.
Raises a ValueError exception if a value of zero provided.
"""
if value == 0:
raise ValueError('Divide by Zero Error')
self.__denominator = value
def __repr__(self):
return str(self.__numerator)+'/'+str(self.__denominator)
def __neg__ (self):
"""Returns a new Fraction equal to the negation of self."""
return Fraction(-self._numerator, self.__denominator)
def __add__ (self, qwerty):
"""Returns a new reduced Fraction equal to self + rfraction."""
numer = self.__numerator * qwerty.getDenominator() + \
qwerty.getNumerator() * self.__denominator
denom = self.__denominator * qwerty.getDenominator ()
resultFrac = Fraction(numer, denom)
return resultFrac
frac3=Fraction(2,3)
frac3.__numerator=5
print(frac3)
frac3.__denominator=0
print(frac3)
print(frac3.__numerator, frac3.__denominator)
OUTPUT :
2/3
2/3
5 0
I am new to Python and started reading about objects and classes. In the first two lines of the output, the fact that members __numerator, __denominator are private was reflected, the assignments 'frac3.__numerator=5' and 'frac3.__denominator=0' had no effect, albeit strange that there was no error generated. In the third line it seems that both private instance variables were printed without errors, why is that?