Gaddis Python 6e Chapter 10
Chapter Overview
Title: Classes and Object-Oriented Programming
Author: Tony Gaddis
Edition: Sixth Edition
Publisher: Pearson Education, Inc.
Copyright Year: 2023
Topics Covered
Procedural and Object-Oriented Programming
Classes
Working with Instances
Techniques for Designing Classes
Procedural Programming
Definition: Writing programs composed of functions performing specific tasks.
Procedures operate on separate data items, often passing data from one procedure to another.
Focus: Create procedures that operate on the program’s data.
Object-Oriented Programming
Definition: Emphasizes creating objects.
Object: An entity containing data (data attributes) and procedures (methods).
Methods perform operations on the data attributes.
Encapsulation: Combining data and code within a single object.
Key Concepts
Data Hiding: Object's data attributes are concealed from outside code; access is restricted to object's methods.
Object Reusability: The same object can be utilized across different programs (e.g., a 3D image object can be used in architecture and game programming).
Everyday Example of an Object
Data Attributes: Define an object's state (e.g., a clock object might store seconds, minutes, hours).
Public Methods: Allow external code to manipulate the object (e.g.,
set_time,set_alarm_time).Private Methods: Used only within the object itself to maintain internal logic.
Classes
Definition: A class specifies the data attributes and methods for a certain type of object, similar to a blueprint or cookie cutter.
Instance: Specific object created from a class (e.g., specific house corresponding to blueprint).
Multiple instances can be created from a single class.
Class Definitions
Start with
class Class_name:; class names typically start with uppercase letters.Method Definition: Similar to standard function definition in Python.
Self Parameter: Required in every class method to reference the current object.
Initializer Method: Automatically executed when an instance is created; defined as
def __init__(self):and initializes data attributes.
Example Code Structure
class Coin:
def __init__(self):
self.sideup = 'Heads'
def toss(self):
if random.randint(0, 1) == 0:
self.sideup = 'Heads'
else:
self.sideup = 'Tails'
def get_sideup(self):
return self.sideupCreating and Using Instances
New instance format:
variable_name = ClassName()(e.g.,my_coin = Coin()).To call methods:
My_instance.method(). Theselfparameter ensures the specific instance is affected.Example:
Line 1: An instance of the Coin class is created;
__init__is called.Line 2: Calls the
my_coinobject'stossmethod.Line 3: Retrieves the current side up of the coin.
Data Attributes and Class Definitions
Data attributes should be private; prefix with
__(e.g.,__sideup).Classes may be stored in modules with a
.pyfilename extension.
The BankAccount Class Example
Attributes: Can include parameters such as balance.
Initializer Method: Receives values for attributes like
__balance.Example method operations, e.g., deposit method for adding amounts.
__str__ Method
Displays an object's state (attributes values) and is automatically invoked by
print()orstr()functions.
Working With Instances
Instance Attribute: Unique to each instance and created using the self parameter.
Each class instance has its own attributes if many instances are created.
Accessor and Mutator Methods
Private attributes should have accessor methods (return values) and mutator methods (change values).
Passing Objects as Arguments
When passing objects, actually pass a reference. Methods of the object can be called, and data attributes can be modified using mutator methods.
Techniques for Designing Classes
UML Diagram: A standard format for depicting object-oriented structures, including class names, data attributes, and methods.
Finding Classes in a Problem
Identify real-world objects mentioned in problem description.
Technique: Extract nouns as potential classes from written descriptions. Refine the list by removing redundancies or irrelevant items.
Identifying Class Responsibilities
Identify data attributes and actions of classes through analysis of the problem domain.
Summary
Discussed procedural vs. object-oriented programming, classes, instance creation, class definitions including the self parameter, hiding data attributes, and designing classes.