{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "###
San Jose State University
Department of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2024
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 10.2.2 `Account` Class Definition\n", "#### Recall our import:\n", "``` Python\n", "from account import Account\n", "```\n", "#### Therefore, we define our custom class `Account` in Python source file `account.py`:\n", "``` Python\n", "from decimal import Decimal\n", "\n", "class Account:\n", " \"\"\"\n", " Account class for maintaining a bank account balance.\n", " \"\"\"\n", " def __init__(self, name, balance):\n", " \"\"\"\n", " Constructor of an Account object.\n", " @param name the account holder's name.\n", " @param balance the initial account balance.\n", " \"\"\"\n", " # If the balance is less than 0.00, raise an exception\n", " if balance < Decimal('0.00'):\n", " raise ValueError('Initial balance must be >= to 0.00.')\n", "\n", " self.name = name\n", " self.balance = balance\n", "\n", " def deposit(self, amount):\n", " \"\"\"\n", " Deposit money to the account.\n", " @param amount the amount to deposit.\n", " \"\"\"\n", " # if amount is less than 0.00, raise an exception\n", " if amount < Decimal('0.00'):\n", " raise ValueError('amount must be positive.')\n", "\n", " self.balance += amount\n", " \n", " def display(self):\n", " \"\"\"\n", " Display the balance.\n", " \"\"\"\n", " print(f'Account for {self.name} has ${self.balance:.2f}')\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Note that `account.py` itself imports class `Decimal` from the `decimal` module. This is an example of **class composition** -- class `Account` is composed of another class, `Decimal`. Computer scientists call this a ***has-a*** relationship -- `Account` has a `Decimal`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Recall that **methods** are functions associated with objects. The methods are defined in the class definition." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Class `Account` defines a special **constructor** method `__init__()` which is automatically called during run time whenever we instantiate (construct) an object of the class:\n", "``` Python\n", "account1 = Account('John Green', Decimal('50.00'))\n", "```\n", "#### The constructor for `Account` initializes the `name` and `balance` **attributes** of the object being constructed. Note that the constructor for a class is always named `__init__` with *double underscores* at the beginning and at the end." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from account import Account\n", "from decimal import Decimal\n", "\n", "account1 = Account('John Green', Decimal('50.00'))\n", "account1.display()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Class `Account` defines two methods for its objects, `deposit()` and `withdraw()`. They look like regular function definitions, but they're defined inside the class definition." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Whenever we call a method on an object, such as\n", "``` Python\n", "account1.deposit(Decimal('25.53'))`\n", "```\n", "#### Pythom *implicitly* passes the object as the first argument to the method. That's why every method definition, including the constructor, has `self` as its first parameter. In each method's code, `self` refers to the object that the method was called on. For example, when we call the `deposit()` method in an `Account` object, `self.balance += amount` adds the value of `amount` to the value of that object's `balance` attribute." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "account1.deposit(Decimal('25.53'))\n", "account1.display()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "##########################################################################\n", "# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n", "# Pearson Education, Inc. All Rights Reserved. #\n", "# #\n", "# DISCLAIMER: The authors and publisher of this book have used their #\n", "# best efforts in preparing the book. These efforts include the #\n", "# development, research, and testing of the theories and programs #\n", "# to determine their effectiveness. The authors and publisher make #\n", "# no warranty of any kind, expressed or implied, with regard to these #\n", "# programs or to the documentation contained in these books. The authors #\n", "# and publisher shall not be liable in any event for incidental or #\n", "# consequential damages in connection with, or arising out of, the #\n", "# furnishing, performance, or use of these programs. #\n", "##########################################################################\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Additional material (C) Copyright 2024 by Ronald Mak" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 4 }