Here are a couple of classes
A parent
class GeometricShape:
def __init__(self, name):
self.set_name(name)
def get_name(self):
return self.__name
def set_name(self,name):
validate_non_empty_string(name)
self.__name = name
def __repr__(self):
return f'GeometricShape(name={self.__name})'
def __eq__(self, other):
return(self.get_name == other.get_name )
A child
class Rectangle(GeometricShape):
def __init__(self, length, width, name='Rectangle'):
#
# the parent class sets the name in the constructor
# the name is not set in the child, and we want the naming behavior
# provided by the parents constructor
super().__init__(name)
#
self.set_length(length)
self.set_width(width)
def get_length(self):
return self.__length
def get_width(self):
return self.__width
def set_length(self,length):
# Check the data
validate_positive_number(length)
self.__length = length
def set_width(self,width):
validate_positive_number(width)
self.__width = width
def get_perimeter (self):
return 2 * self.__length + 2 * self.__width
def get_area (self):
return self.__length * self.__width
def __repr__(self):
return f'Rectangle(a={self.__length}, b={self.__width})'
# Attempted Solution 1
# in __eq__
# return( (self.__width == other.get_width() ) and (self.__length == other.get_length() ))
# AttributeError: 'NoneType' object has no attribute 'get_width'
#
def __eq__(self, other):
return( (self.__width == other.get_width() ) and (self.__length == other.get_length() ))
# Attempted Solution 2
# produces this error
# ---------------------------------------------------------------------------------------
# line 116, in __eq__
# return( (self.__width == other.get_width() ) and (self.__length == other.get_length() ))
# ---------------------------------------------------------------------------------------
#
#def __eq__(self, other):
# return( (self.__width == other.__width ) and (self.__length == other.__length ))
I have attempted two solutions for the eq method, both fail in different ways the corresponding errors produced by each are given.
The tests for the launch of the ==
are being called from encrypted pye files.
Any ideas?