MIT 6.100L Introduction to CS and Programming Using Python, Fall 2022 · Problem set 4, Part B, 2.2) Message — __init__ and get_text · Dr. Ana Bell · CC BY-NC-SA 4.0 · MIT publishes no solution for this set; the worked solution is ours
Write the constructor __init__(self, input_text) and the getter get_text(self) of MIT's Message class, the class the one time pad project is built on.
This is your first experience creating your own classes! In this problem set a parent class called Message contains the methods that every kind of message will need to use: for example, a method to get the text of the message. (MIT's problem set goes on to build two child classes on Message, PlaintextMessage and EncryptedMessage. They need inheritance, so they come in a later unit.)
Fill in these two methods of the Message class according to the specifications in their docstrings:
__init__(self, input_text) initializes a Message object. input_text (a string) is the message's text, and a Message object has one attribute: the message text.get_text(self) is used to access the message text outside of the class. It returns the message text (a string).MIT has also implemented a __repr__ method so that when you print out your Message objects, the console displays a nice human readable result. Please do not change it.
msg = Message('testing message') print(msg.get_text()) # testing message print(Message('he,(llo)').get_text()) # he,(llo) print(msg) # Message('testing message')