Creating Email Message Class in Python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Creating Email Message Class in Python (/thread-28475.html) |
Creating Email Message Class in Python - QuavoJ - Jul-20-2020 My homework is creating an e-mail Message class (we create an e-mail Message class that models a basic email application). The first step in the homework is to define the Message class which includes a sender, recipient, and body. The constructor of this Message class should have the parameters self, sender, and recipient, with values set for sender and recipient and body set as an empty string. This is what I have so far and I think I am on the right track: class Message: def __init__(self, sender, recipient): self.sender = sender self.recipient = recipient p1 = Message("From: John Doe", "To: Jane Doe") print(p1.sender) print(p1.recipient)The return output is simply: From: John Doe To: Jane Doe My teacher did not explain a lot of the other stuff in class so I need help with a couple of things. First, I need help setting up an empty string for the body. I also need to create an "append" method with the parameters "self" and "line" ("line" contains the line of text to add to the body of the email message) and I'm not really sure how to do that. Additionally, the teacher has asked us to create a method to_string that returns a string representation of the entire message and each email message line should be ended with a newline character "\" (no clue what this even means). We also need a helper method str_ok() that validates str parameters. Some program specifications: Use mutators set_sender(...), and set_recipient(...) for the sender and recipient instance members Use append() method for the body instance member When the set methods detect an invalid string, the mutator returns False and a new default string is stored in that member When the append method detects an invalid string, no action should be taken Helper methods: string_to_string() def str_ok() (returns True if string's length is <= MAX_LEN (inclusive) and returns False otherwise) Thank you so much! RE: Creating Email Message Class in Python - Yoriz - Jul-20-2020 an empty string is as simple as '' to add an empty string as a attribute body self.body = '' a newline character is \n Tutorials on classes https://python-forum.io/Thread-Classes-Class-Basics https://docs.python.org/3/tutorial/classes.html |