Session Ready
Exercise

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.

Instructions 1/4
undefined XP
  • 1
  • 2
  • 3
  • 4

Create a Customer class with the __init__() method that:

  • takes parameters name and new_bal,
  • assigns name to the attribute name,
  • raises a ValueError if new_bal is negative,
  • otherwise, assigns new_bal to the attribute _balance (with _).