Get startedGet started for free

Create and set properties

There are two parts to defining a property:

  • first, define an "internal" attribute that will contain the data;
  • then, define a @property-decorated method whose name is the property name, and that returns the internal attribute storing the data.

If you'd also like to define a custom setter method, there's an additional step:

  • define another method whose name is exactly the property name (again), and decorate it with @prop_name.setter where prop_name is the name of the property. The method should take two arguments -- self (as always), and the value that's being assigned to the property.

In this exercise, you'll create a balance property for a Customer class - a better, more controlled version of the balance attribute that you worked with before.

This exercise is part of the course

Object-Oriented Programming in Python

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create a Customer class
____
Edit and Run Code