Title: Introduction%20to%20Computer%20Programming%20Chapter%208:%20Software%20Objects
1Introduction to Computer Programming Chapter 8
Software Objects
- Michael Scherger
- Department of Computer Science
- Kent State University
2Contents
- Object oriented programming
- Creating classes, methods and objects
- Constructors
- Attributes
- Class attributes and static methods
- Object encapsulation
- Private attributes and methods
- Controlling attribute access
3Critter Caretaker
- Example Critter Caretaker Program
4Object 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
5Creating 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
6Creating 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, Im an instance of a Critter Class
7Creating 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()
8Creating 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()
9Creating Classes, Methods, and Objects
- Example Simple Critter Program
10Constructors
- 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
11Constructors
- Creating Multiple Objects
- Once your class has been defined, your program
can create multiple objects - Example
- main program
- crit1 Critter()
- crit2 Critter()
- crit1.talk()
- crit2.talk()
- A critter has been born!
- A critter has been born!
- Im an instance of class Critter
- Im an instance of class Critter
12Constructors
- Example Constructor Critter Program
13Attributes
- 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
14Attributes
- Accessing Attributes
- Attributes are accessed in the same manner
methods are accessedusing dot notation - Example
- print crit1.name
15Attributes
- Printing an Object
- By default, printing an object in Python would
print something very cryptic - lt__main__.Critter object at 0x0123ABCFgt
- A class can define a special print method
called __str__ that can be used to print a class - Example
- def __str__(self)
- print Hi, Im, self.name, \n
16Attributes
- Example Attribute Critter Program
17Class 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
18Class Attributes and Static Methods
- Example Classy Critter Program
19Class Attributes and Static Methods
- Creating a Class Attribute
- Example
- class AnyClass(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
20Class 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 AnyClass.total
21Class 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
22Class 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
23Object Encapsulation
- Just as functions encapsulate details about a
procedure, objects also encapsulate details into
a single container-like structure - Example checking accounts
24Private Attributes and Methods
- Example Private Critter Program
25Private 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
26Private 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
27Private 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 AttributeError
- print crit.__mood error AttributeError
- print crit._Critter__mood OK
28Private 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
29Private 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
30Private Attributes and Methods
- Respecting an Objects Privacy
- It is good programming practice to behave
yourself and respect the private and public
properties of an attribute or method
31Private Attributes and Methods
- Understanding When to Implement Privacy
- When you create a class
- Create methods so that clients wont 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 objects
attributes - Avoid directly altering an objects attributes
- Never directly access an objects private
attributes or methods
32Controlling Attribute Access
- Example Property Critter Program
33Controlling Attribute Access
- Using Get Methods
- Rather than reading an objects attributes
directly, implement a get method that returns
the value of the attribute - Make the attribute privatebut 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
34Controlling Attribute Access
- Using Set Methods
- Rather than writing to an objects attribute
directly, implement a set method that writes
the value to the attribute - Make the attribute privatebut the function to
update the value public - Example
- def set_name(self, new_name)
- if new_name
- print A critters name cannot be empty
- else
- self.__name new.name
- print Name change successful
35Controlling 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)
36Controlling Attribute Access
- Using Properties
- Example
- print crit.name calls the get_name method
- crit.name George calls the set_name method
37Critter Caretaker (Again)
- Example Critter Caretaker Program