I am working on the class which prints different messages in english. I want to create another class with exactly the same functionality, but with messages in different language.
As I learned sth about subclasses recently I wanted to get some practice and rewrote my Class() methods to have all messages saved in variables, which I tried to override in NewClass(Class). Here is the shortcut of my code:
class Class:
def __init__(self, some_other_arguments):
self.msg_1 = 'message 1'
if some_condition:
raise KeyError(self.msg_1)
class NewClass(Class):
def __init__(self, some_other_arguments):
super().__init__(some_other_arguments)
self.msg_1 = 'message 2'
object_1 = Class(some_arguments) #should print 'message 1'
#prints 'message 1'
object_2 = NewClass(some_argiments) #should print 'message 2'
#prints 'message 1'
At first I wanted to copy first class, rename it and change the messages. But this will produce redundant code which I would like to avoid. What am I doing wrong?
class Class:
def __init__(self):
self.msg_1 = 'message 1'
print(self.msg_1)
class NewClass(Class):
def __init__(self):
super().__init__()
self.msg_1 = 'message 2'
object_1 = Class() #prints 'message 1'
object_2 = NewClass() #prints 'message 1'