Advertisement
SimeonTs

SUPyF2 Objects/Classes-Exericse - 07. Articles

Oct 19th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.47 KB | None | 0 0
  1. """
  2. Objects and Classes - Exericse
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1734#6
  4.  
  5. SUPyF2 Objects/Classes-Exericse - 07. Articles
  6.  
  7. Problem:
  8. Create a class Article. The __init__ method should accept 3 arguments: title, content, author.
  9. The class should also have 4 methods:
  10. • edit(new_content) - changes the old content to the new one
  11. • change_author(new_author) - changes the old author to with the new one
  12. • rename(new_title) - changes the old title with the new one
  13. • __repr__() - returns the following string "{title} - {content}: {author}"
  14.  
  15. Example:
  16.  
  17. Test Code:
  18. article = Article("some title", "some content", "some author")
  19. article.edit("new content")
  20. article.rename("new title")
  21. article.change_author("new author")
  22. print(article)
  23.  
  24. Output:
  25. new title - new content: new author
  26. """
  27.  
  28.  
  29. class Article:
  30.     def __init__(self, title: str, content: str, author: str):
  31.         self.title = title
  32.         self.content = content
  33.         self.author = author
  34.  
  35.     def edit(self, new_content):
  36.         self.content = new_content
  37.  
  38.     def change_author(self, new_author):
  39.         self.author = new_author
  40.  
  41.     def rename(self, new_title):
  42.         self.title = new_title
  43.  
  44.     def __repr__(self):
  45.         return f"{self.title} - {self.content}: {self.author}"
  46.  
  47.  
  48. article = Article("some title", "some content", "some author")
  49. article.edit("new content")
  50. article.rename("new title")
  51. article.change_author("new author")
  52. print(article)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement