Introduction to Computer Programming Chapter 8 Software Objects

Introduction to Computer Programming Chapter 8: Software Objects Michael Scherger Department of Computer Science Kent State University November 28, 2005 ICP: Chapter 8: Software Objects 1

Contents • • Object oriented programming Creating classes, methods and objects Constructors Attributes Class attributes and static methods Object encapsulation Private attributes and methods Controlling attribute access November 28, 2005 ICP: Chapter 8: Software Objects 2

Critter Caretaker • Example: Critter Caretaker Program November 28, 2005 ICP: Chapter 8: Software Objects 3

Object Oriented Programming • OOP is a different way of thinking about programming • Basic building block is an software object • Objects are created from classes – A template or blueprint for making objects – Two objects built from the same class can have different values for their attributes • Classes define attributes and methods November 28, 2005 ICP: Chapter 8: Software Objects 4

Creating Classes, Methods, and Objects • Defining a Class – A class must be defined before any objects can be created from it – Classes start with the reserved word “class” – Example class Critter(object): “”” This is a Critter object “”” November 28, 2005 ICP: Chapter 8: Software Objects 5

Creating Classes, Methods, and Objects • Defining a Method – A method is a type of function used by objects • Methods have similar syntax structure to functions – Methods have a special parameter called “self” by convention • It provides a way for a method to refer to the object itself – Example def talk( self ): print “Hi, I’m an instance of a Critter Class” November 28, 2005 ICP: Chapter 8: Software Objects 6

Creating Classes, Methods, and Objects • Instantiating an Object – Once your class (blueprint) is finalized, you must create an instance of the class (an object) – This instructs Python to use the blueprint and creates an object – Example crit = Critter() November 28, 2005 ICP: Chapter 8: Software Objects 7

Creating Classes, Methods, and Objects • Invoking a Method – Once the object has been instantiated, you can “access” the attributes and “invoke” the methods – Attributes and methods can be accessed using “dot notation” – Example crit. talk() November 28, 2005 ICP: Chapter 8: Software Objects 8

Creating Classes, Methods, and Objects • Example: Simple Critter Program November 28, 2005 ICP: Chapter 8: Software Objects 9

Constructors • Creating a Constructor – A constructor is a special method that is automatically invoked right after the new object is created • A start up method usually used to initialize attribute values of an object – Example def __init__(self, name): print “A new critter has been born” November 28, 2005 ICP: Chapter 8: Software Objects 10

Constructors • Creating Multiple Objects – Once your class has been defined, your program can create multiple objects – Example # main program crit 1 = Critter() crit 2 = Critter() crit 1. talk() crit 2. talk() • • November 28, 2005 A critter has been I’m an instance of Critter ICP: Chapter 8: Software Objects born! class 11

Constructors • Example: Constructor Critter Program November 28, 2005 ICP: Chapter 8: Software Objects 12

Attributes • Initializing Attributes – Attributes are special properties of an object that are defined by a class – Example def __init__(self, name): print “A new critter has been born!” self. name = name November 28, 2005 ICP: Chapter 8: Software Objects 13

Attributes • Accessing Attributes – Attributes are accessed in the same manner methods are accessed…using “dot notation” – Example • print crit 1. name November 28, 2005 ICP: Chapter 8: Software Objects 14

Attributes • Printing an Object – By default, printing an object in Python would print something very cryptic • <__main__. Critter object at 0 x 0123 ABCF> – A class can define a special “print method” called __str__ that can be used to print a class – Example def __str__(self): print “Hi, I’m”, self. name, “n” November 28, 2005 ICP: Chapter 8: Software Objects 15

Attributes • Example: Attribute Critter Program November 28, 2005 ICP: Chapter 8: Software Objects 16

Class Attributes and Static Methods • Creating a Class Attribute – Sometimes your class definition need to have attributes and methods that are shared by all objects of that class • Example is a count of the number of objects of a class (reference count) – Class attributes are any assignment statements in a class definition yet outside of any class method November 28, 2005 ICP: Chapter 8: Software Objects 17

Class Attributes and Static Methods • Example: Classy Critter Program November 28, 2005 ICP: Chapter 8: Software Objects 18

Class Attributes and Static Methods • Creating a Class Attribute – Example class Any. Class(object): total = 0 ### other class methods. . . – A class attribute assignment statements are only executed once • When Python first sees the definition • Class attributes can be used without any objects created November 28, 2005 ICP: Chapter 8: Software Objects 19

Class Attributes and Static Methods • Accessing a Class Attribute – Accessing a class attribute is the same as accessing a regular attribute • Use dot notation – Example print Any. Class. total November 28, 2005 ICP: Chapter 8: Software Objects 20

Class Attributes and Static Methods • Creating a Static Method – A static method is a method common to all objects • Does not contain the reserved word “self” in the parameter list • Uses the function staticmethod() to declare that a function is a static method – See example in Classy Critter Program November 28, 2005 ICP: Chapter 8: Software Objects 21

Class Attributes and Static Methods • Invoking a Static Method – Static methods can be invoked in the same manner as attributes and other methods • Uses “dot notation” • Can be invoked prior to any object instantiation November 28, 2005 ICP: Chapter 8: Software Objects 22

Object Encapsulation • Just as functions encapsulate details about a procedure, objects also encapsulate details into a single “container -like” structure • Example: checking accounts November 28, 2005 ICP: Chapter 8: Software Objects 23

Private Attributes and Methods • Example: Private Critter Program November 28, 2005 ICP: Chapter 8: Software Objects 24

Private Attributes and Methods • Creating Private Attributes – Many times the designer of a class would like the some (or most) of the attributes and methods to be private or not directly accessible to the user – Attributes can be either public or private • public attributes are directly accessible by the client • private attributes are not directly accessible by the client – some other class method must access them November 28, 2005 ICP: Chapter 8: Software Objects 25

Private Attributes and Methods • Creating Private Attributes – Private attributes are created by prepending two underscores in front of the attribute name – Example def __init__(self, name, mood): print “A new critter has been born!” self. name = name # public attribute self. __mood = mood # private attribute November 28, 2005 ICP: Chapter 8: Software Objects 26

Private Attributes and Methods • Accessing Private Attributes – Private attributes can only be accessed inside the class definition – Example crit = Critter( name = “Chelsea”, mood = “grumpy”) print crit. mood # error Attribute. Error print crit. __mood # error Attribute. Error print crit. _Critter__mood # OK November 28, 2005 ICP: Chapter 8: Software Objects 27

Private Attributes and Methods • Creating Private Methods – Similar to private attributes, private methods have two underscores prepended in front of them – Example def __some_private_method(self): print “This is a private method” November 28, 2005 ICP: Chapter 8: Software Objects 28

Private Attributes and Methods • Accessing Private Methods – Private methods can only be invoked inside a class definition – Example crit = Critter( name = “Chelsea”, mood = “grumpy”) print crit. some_private_method # NO print crit. __some_private_method # NO print crit. _Critter__some_private_method # OK November 28, 2005 ICP: Chapter 8: Software Objects 29

Private Attributes and Methods • Respecting an Object’s Privacy – It is good programming practice to “behave yourself” and respect the private and public properties of an attribute or method November 28, 2005 ICP: Chapter 8: Software Objects 30

Private Attributes and Methods • Understanding When to Implement Privacy – When you create a class • Create methods so that clients won’t need to directly access an objects attributes • Use privacy sparingly and only for those few attributes and methods that are completely internal to the operation of the object – When you use an object • Minimize the direct reading of an object’s attributes • Avoid directly altering an object’s attributes • Never directly access an object’s private attributes or methods November 28, 2005 ICP: Chapter 8: Software Objects 31

Controlling Attribute Access • Example: Property Critter Program November 28, 2005 ICP: Chapter 8: Software Objects 32

Controlling Attribute Access • Using Get Methods – Rather than reading an object’s attributes directly, implement a “get” method that returns the value of the attribute • Make the attribute private…but the function to retrieve the value public – Example def __init__(self, name): print “A new critter has been born!” self. __name = name def get_name(self): return self. __name November 28, 2005 ICP: Chapter 8: Software Objects 33

Controlling Attribute Access • Using Set Methods – Rather than writing to an object’s attribute directly, implement a “set” method that writes the value to the attribute • Make the attribute private…but the function to update the value public – Example def set_name(self, new_name): if new_name == “”: print “A critter’s name cannot be empty” else: self. __name = new. name print “Name change successful” November 28, 2005 ICP: Chapter 8: Software Objects 34

Controlling Attribute Access • Using Properties – A property allow you to use the access methods while hiding the details from the user • It “wraps” the access methods around the consistent dot notation – Example # define a class # define access methods get and set name = property( get_name, set_name) November 28, 2005 ICP: Chapter 8: Software Objects 35

Controlling Attribute Access • Using Properties – Example print crit. name # calls the get_name method crit. name = “George” # calls the set_name method November 28, 2005 ICP: Chapter 8: Software Objects 36

Critter Caretaker (Again) • Example: Critter Caretaker Program November 28, 2005 ICP: Chapter 8: Software Objects 37
- Slides: 37