Hone logo
Hone
Problems

Python Property Setter Challenge: Secure Your Data

Object-oriented programming in Python allows you to encapsulate data within classes. However, directly modifying attributes can lead to inconsistencies or invalid states. Property setters provide a controlled way to update attributes, enabling validation and other logic before a value is assigned. This challenge will test your understanding of how to implement custom setters for class attributes.

Problem Description

You need to create a Python class BankAccount that represents a simple bank account. This class should have an attribute balance which represents the current amount of money in the account. The primary goal is to implement a property setter for the balance attribute that enforces specific rules:

  • The balance can only be set to a non-negative numerical value.
  • If an attempt is made to set the balance to a negative value, a ValueError should be raised with a descriptive message.
  • If an attempt is made to set the balance to a non-numerical value, a TypeError should be raised with a descriptive message.
  • The initial balance should also be set using the same validation logic.

You should use Python's @property decorator for the getter and a corresponding @<attribute_name>.setter decorator for the setter.

Examples

Example 1:

account = BankAccount(100.50)
print(account.balance)
account.balance = 250.75
print(account.balance)

Output:

100.5
250.75

Explanation: The initial balance is set to 100.50. Then, the balance is updated to 250.75, which is a valid operation.

Example 2:

account = BankAccount(50)
try:
    account.balance = -20
except ValueError as e:
    print(e)

Output:

Balance cannot be negative.

Explanation: An attempt to set a negative balance raises a ValueError.

Example 3:

account = BankAccount(100)
try:
    account.balance = "invalid"
except TypeError as e:
    print(e)

Output:

Balance must be a number.

Explanation: An attempt to set a non-numerical balance raises a TypeError.

Constraints

  • The balance attribute must be a number (integer or float).
  • The balance attribute must always be non-negative (>= 0).
  • The class must be named BankAccount.
  • The attribute to manage is balance.

Notes

  • You'll need to use a private attribute (e.g., _balance) to store the actual balance internally, and expose it through a public balance property.
  • Consider how to handle the initial setting of the balance within the __init__ method. It should also pass through the validation logic.
Loading editor...
python