{ "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.3 Controlling Access to Attributes " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Using classes not only modularizes our Python programs, but they can also increase their reliability. One way to increase reliability is to control the access to an object's attributes to help prevent badly behaved programs from setting the attributes to bad values." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from account import Account\n", "from decimal import Decimal" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "account1 = Account('John Green', Decimal('50.00'))\n", "account1.balance" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### An example of bad behavior:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "account1.balance = Decimal('-1000.00')\n", "account1.balance" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Encapsulation \n", "#### Computer scientists say that a class **encapsulates** related attributes and methods. Therefore, the `Account` class encapsulates the `name` and `balance` attributes and the `deposit()` and `withdraw()` methods that operate on `balance`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Leading Underscore (`_`) Naming Convention\n", "#### Whereas we can write a method to validate its parameter values, there is no way to prevent badly behaved code to set a object's attribute to a bad value by directly accessing the attribute, such as `account1.balance`. Therefore, Python relies on a ***convention***. If an attribute's name starts with an underscore, such as `_hour`, then that attribute is supposedly ***private***. A programmer should ***not*** write code that's outside of a class that accesses private attributes directly. In other words, only the methods of a class should be able to access that class's private attributes." ] }, { "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": "markdown", "metadata": {}, "source": [ "#### Additional material (C) Copyright 2023 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 }