Editor

Create a Read-Only Name Property

Beginner · Write the code · from Python @property: Getters, Setters, and Computed Attributes

From the lesson: Python @property: Getters, Setters, and Computed Attributes

Imagine a thermostat on your wall. You set the temperature to 72°F and the system heats your house. But what if someone sets it to 5000°F? The house wouldn't literally reach that temperature, but your heating system would run forever trying. A smart thermostat would reject that value immediately.

Read the lesson

Create a class Person with an __init__ that takes a name parameter and stores it in self._name. Add a read-only @property called name that returns self._name. Then create a Person with the name 'Alice' and print their name.

# Create the Person class with a read-only name property

More @property exercises

All Python exercises · 50 practice problems · Object-Oriented Programming quiz