This chapter discusses the differences between procedural and object-oriented programming, focusing on the concept of classes and instances. It covers class definitions, the use of the self parameter, data attributes, methods, and the importance of encapsulation and data hiding. Additionally, it highlights techniques for designing classes and storing them in modules.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views31 pages
Gaddis Python 3e Chapter 10
This chapter discusses the differences between procedural and object-oriented programming, focusing on the concept of classes and instances. It covers class definitions, the use of the self parameter, data attributes, methods, and the importance of encapsulation and data hiding. Additionally, it highlights techniques for designing classes and storing them in modules.
Procedural Programming Procedural programming: writing programs made of functions that perform specific tasks Procedures typically operate on data items that are separate from the procedures Data items commonly passed from one procedure to another Focus: to create procedures that operate on the program’s data
Class Definitions Class definition: set of statements that define a class’s methods and data attributes Format: begin with class Class_name: Class names often start with uppercase letter Method definition like any other python function definition self parameter: required in every method in the class – references the specific object that the method is working on
Class Definitions (cont’d.) Initializer method: automatically executed when an instance of the class is created Initializes object’s data attributes and assigns self parameter to the object that was just created Format: def __init__ (self): Usually the first method in a class definition
Class Definitions (cont’d.) To create a new instance of a class call the initializer method Format: My_instance = Class_Name() To call any of the class methods using the created instance, use dot notation Format: My_instance.method() Because the self parameter references the specific instance of the object, the method will affect this instance Reference to self is passed automatically
Hiding Attributes and Storing Classes in Modules An object’s data attributes should be private To make sure of this, place two underscores (__) in front of attribute name Example: __current_minute Classes can be stored in modules Filename for module must end in .py Module can be imported to programs that use the class
The BankAccount Class – More About Classes Class methods can have multiple parameters in addition to self For __init__, parameters needed to create an instance of the class Example: a BankAccount object is created with a balance When called, the initializer method receives a value to be assigned to a __balance attribute For other methods, parameters needed to perform required task Example: deposit method amount to be deposited
The __str__ method Object’s state: the values of the object’s attribute at a given moment __str__ method: displays the object’s state Automatically called when the object is passed as an argument to the print function Automatically called when the object is passed as an argument to the str function
Working With Instances Instance attribute: belongs to a specific instance of a class Created when a method uses the self parameter to create an attribute If many instances of a class are created, each would have its own set of attributes
Finding the Classes in a Problem (cont’d.) 1. Get written description of the problem domain May be written by you or by an expert Should include any or all of the following: Physical objects simulated by the program The role played by a person The result of a business event Recordkeeping items
Finding the Classes in a Problem (cont’d.) 2. Identify all nouns in the description, each of which is a potential class Should include noun phrases and pronouns Some nouns may appear twice
Finding the Classes in a Problem (cont’d.) 3. Refine the list to include only classes that are relevant to the problem Remove nouns that mean the same thing Remove nouns that represent items that the program does not need to be concerned with Remove nouns that represent objects, not classes Remove nouns that represent simple values that can be assigned to a variable
Identifying a Class’s Responsibilities A classes responsibilities are: The things the class is responsible for knowing Identifying these helps identify the class’s data attributes The actions the class is responsible for doing Identifying these helps identify the class’s methods To find out a class’s responsibilities look at the problem domain Deduce required information and actions
Summary This chapter covered: Procedural vs. object-oriented programming Classes and instances Class definitions, including: The self parameter Data attributes and methods __init__ and __str__ functions Hiding attributes from code outside a class Storing classes in modules Designing classes