{ "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": [ "# Object-Oriented Programming" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 10.2.1 Test-Driving Class `Account` \n", "#### Object-oriented programming (OOP) is the programming paradigm of many modern languages such as Java, C++, and Python." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Datatype\n", "#### Recall that a **datatype** is an attribute of a value. It determines:\n", "- #### What it can be (integer, float, or decimal number, string, list, numpy array, etc.)\n", "- #### What operations it can work on it (addition, concatenation, substring, etc.)\n", "- #### How it is printed or displayed, or how it can be formatted." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Objects\n", "#### We've used values that are **objects**, such as strings.\n", "#### Objects have special functions called **methods** that we can call on the object. To make a method call on an object value, follow the value (which can be in a variable) with a dot followed by the method name. The method call can have arguments. For example, if variable `str` has a string value:\n", "- #### `str.strip()` returns a new string without leading and trailing whitespace.\n", "- #### `str.count('to', 14)` returns the number of times `to` is in the string starting at index position 14." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Classes\n", "#### Like other OOP languages, Python allows us to create a custom datatype by defining a **classs**. A class is model or blueprint for creating new values of the class datatype during run time. These values are objects. We **instantiate** an object when we create the object from its class." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Every value is an object\n", "#### In Python, **every** value is an object. Of course, a string is an object, but so is an integer or a float number. Therefore, Python comes with many built-in, predefined classes, such as `string`, `int`, and `float`. Each class's objects has methods that we can call on the objects. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "12.5.hex()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 12.5\n", "print(f'{x.as_integer_ratio() = }')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f'{12.5.as_integer_ratio() = }')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### We can also import classes from modules such as `Decimal`. There are many classes we can import from the Python Standard Library. There are also contributed open-source modules that we can import to use their classes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Importing Classes Account and Decimal\n", "#### In this example, we import custom class `Account` from module `account` defined in the Python source file `account.py` and class `Decimal` from module `decimal` in the Python Standard Library.\n", "#### We'll examine the definition of class `Account` after we see how it's used." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from account import Account\n", "from decimal import Decimal" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create an `Account` Object with a Constructor Expression\n", "#### To instantiate (create) an object from a class, use the name of the class followed by any required arguments." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "account1 = Account('John Green', Decimal('50.00'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Getting an `Account`’s Name and Balance\n", "#### A class defines both methods and attributes for its objects. When we instantiated the `Account` object, we initialized its `name` and `balance` attributes. In doing so, we also instantiated and initialized a `Decimal` object." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "account1.name" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "account1.balance" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Depositing Money into an `Account` \n", "#### Class `Account` defines a `deposit()` method for its objects." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "account1.deposit(Decimal('25.53'))\n", "account1.balance" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `Account` Methods Perform Validation\n", "#### A class can define a method to perform validation." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "account1.deposit(Decimal('-123.45'))" ] }, { "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" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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 }