Object-oriented programming · 13 / 26 Exercise Medium

Constructors and destructors

Objective

Create a Python program that asks the user for three names and stores them in a list of objects of the Person class.

Instructions

  • Create a class called Person with an attribute called name.
  • Implement the constructor __init__ of the Person class, which receives the name as a parameter.
  • Include a "destructor" (__del__) that sets the name to an empty text.
  • Define the special method __str__ to display the person as text: Hello! My name is followed by the name.
  • In the main program, ask the user for three names, store them in a list of Person objects and display each person on the screen.
Input
Juan
Sara
Carlos
Output
Hello! My name is Juan
Hello! My name is Sara
Hello! My name is Carlos

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise