Top Banner
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
40

Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Dec 28, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Chapter 11Introduction to Classes

Intro to Computer Science

CS1510, Section 2

Dr. Sarah Diesburg

Page 2: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Lab 12

Let’s review a solution

Page 3: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

What is a Class?

The short answer is that object oriented programming is a way to think about “objects” in a program (such as variables, functions, etc).

A program becomes less a list of instructions and more a set of objects and how they interact.

Page 4: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

OOP Principles

Encapsulation: hiding design details to make the program clearer and more easily modified later.

Modularity: the ability to make objects “stand alone” so they can be reused (our modules). I.e. the math module.

Inheritance: create a new object by inheriting (like father to son) many object characteristics while creating or over-riding for this object.

Polymorphism: (hard) Allow one message to be sent to any object and have it respond appropriately based on the type of object it is.

Page 5: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Class versus Instance

A class is used as a definition to create new instances of those classes (which are objects) Describes what the object is going to look like

Page 6: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Why a Class?

We make classes because we need more complicated, user-defined data types to construct instances we can use.

Each class has potentially two aspects: the data (types, number, names) that each

instance might contain the messages that each instance can respond to

Page 7: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

A First Class

Page 8: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

class Student(object):

"""Simple Student class."""

def __init__(self,first='', last='', id=0): # init instance

self.firstNameStr = first

self.lastNameStr = last

self.idInt = id

def __str__(self): # string representation for printing

return "{} {}, ID:{}".format\

(self.firstNameStr, self.lastNameStr, self.idInt)

Page 9: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Constructor

When a class is defined, a function is made with the same name as the class

This function is called the constructor. By calling it, you can create an instance of the class.

We can affect this creation (more later), but by default Python can make an instance.

Page 10: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

A Class is a New Type

>>>myLst = [1, 2, 3]

type(myLst) => type<‘list’>

>>>myStr = ‘abc’

type(myStr) => type<‘str’>

>>>s1 = Student(“Sarah",“Diesburg",12345)type(s1)

<class '__main__.Student'>

Page 11: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Instance Knows its Class

Because each instance has as its type the class that it was made from, an instance “remembers” its class.

This is often called the “instance-of” relationship.

Page 12: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

“Dot” Reference

We can refer to the attributes of an object by doing a “dot” reference, of the form:

object.attribute The attribute can be a variable or a function. It is part of the object, either directly or by that

object being part of a class.

Page 13: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Accessor Methods

A method that returns information about the STATE of the object. (The value of one or more instance variables).

Page 14: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Two ways to access data

print myInst.myVal print a variable associated with the object myInst

myInst.myMethod() call a method associated with the object myInst

Variable versus method, you can tell by the parenthesis at the end of the reference

Page 15: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

class Student(object):

"""Simple Student class."""

def __init__(self,first='', last='', id=0): # init instance

self.firstNameStr = first

self.lastNameStr = last

self.idInt = id

def __str__(self): # string rep, e.g. for printing

return "%s %s, ID:%s" % \

(self.firstNameStr, self.lastNameStr, self.idInt)

def getID(self):

return self.id

Page 16: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Why I prefer accessor methods over direct access print s1.idInt print s1.getID()

Both return the same exact value right now, but I think that the accessor method is a better way to do this.

Any idea why?

Page 17: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Mutator Methods

Methods that have the potential to change the state of an object.

Page 18: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

class Student(object):

def __init__(self,first='', last='', id=0):

self.firstNameStr = first

self.lastNameStr = last

self.idInt = id

def update(self,first='',last='',id=0):

if first:

self.firsNameStr = first

if last:

self.lastNameStr = last

if id:

self.idInt = id

def __str__(self): # string rep, e.g. for printing

return "%s %s, ID:%s" % \

(self.firstNameStr, self.lastNameStr, self.idInt)

Page 19: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Method versus Function

Discussed before, a method and a function are closely related. They are both “small programs” that have parameters, perform some operation and (potentially) return a value.

The main difference is that methods are functions tied to a particular object.

Page 20: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Difference in Calling

Functions are called, methods are called in the context of an object:function:

dosomething(param1)method: anObject.doSomething(param1)

This means that the object that the method is called on is always implicitly a parameter!

Page 21: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

More on Self

self is an important variable. In any method it is bound to the object that called the method.

Through self, we can access the internal structure of the instance.

Page 22: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

self is Bound for Us

When a dot method call is made, the object that called the method is automatically assigned to self.

We can use self to remember, and therefore refer, to the calling object.

To reference any part of the calling object, we must always precede it with self.

The method can be written generically, dealing with all calling objects through self.

Page 23: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Writing a class

Page 24: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Constructor

There are some special methods that have certain pre-defined roles for all classes.

One of the first we will learn is a constructor. Constructor is called when an instance is

made and provides the class designer the opportunity to set up the instance with variables, by assignment.

Page 25: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Calling a Constructor

As mentioned, a constructor is called by using the name of the class as a function call (by adding () after the class name):

myInst = myClass()

Creates a new instance using myClass.

Page 26: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Special Python Keywords

Again, Python has special uses for keywords that begin and end with __.

so far we have seen the __doc__ attributed of a function with a doc string.

In classes, we will see more of these special values.

Page 27: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

The __init__ Method

One of the special method names in a class is the constructor name, __init__.

By assigning values in the constructor, every instance will start out with the same variables.

You can also pass arguments to a constructor through its init method.

Page 28: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

def __init__(self,first='', last='', id=0):

self.firstNameStr = first

self.lastNameStr = last

self.idInt = id

self is bound to the default instance as it is being made.

If we want to add an attribute to that instance, we modify the attribute associated with self.

Page 29: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Page 30: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Default Constructor

If you don’t provide a constructor then only the default constructor is provided.

The default constructor does “system stuff” to create the instance, nothing more.

You cannot pass arguments to the default constructor.

Page 31: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Every Class Should Have __init__ By providing the constructor, we ensure that

every instance, at least at the point of construction, is created with the same contents.

This gives us some control over each instance.

Page 32: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Printing

def __str__(self):

return "%s %s, ID:%s" % \

(self.firstNameStr, self.lastNameStr, self.idInt)

What happens when we call print myInst? This is assumed, by Python, to be a call to “convert

the instance to a string”, which is the __str__ method.

In the method, myInst is bound to self, and printing then occurs using that instance.

Page 33: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Now There are Three

There are now three groups in our coding scheme: user programmer, class user programmer, class designer

Page 34: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Class Designer

The class designer is creating code to be used by other programmers.

In so doing, the class designer is making a kind of library that other programmers can take advantage of.

Page 35: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Remember this:OOP Principles Encapsulation: hiding design details to make the

program clearer and more easily modified later. Modularity: the ability to make objects “stand alone”

so they can be reused (our modules). I.e. the math module.

Inheritance: create a new object by inheriting (like father to son) many object characteristics while creating or over-riding for this object.

Polymorphism: (hard) Allow one message to be sent to any object and have it respond appropriately based on the type of object it is.

Page 36: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

We are Still at Encapsulation Features of encapsulation:

- hid details of the implementation so that the program was easier to read and write

- modularity: make an object so that it can be reused in other contexts

- providing an interface (the methods) that are the approved way to deal with the class

Page 37: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Private Values

Page 38: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Private Variables in an Instance Many OOP approaches allow you to make a

variable or function in an instance private. Private means not accessible by the class

user, only the class developer. There are advantages to controlling who can

access the instance values.

Page 39: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

‘Privacy’ in Python

Python takes the approach “We are all adults here.” No hard restrictions.

Provides naming to avoid accidents. Use __ in front of any variable

This ‘mangles’ the name to include the class, namely __var becomes _class__var.

Still fully accessible, and the __dict__ makes it obvious.

Page 40: Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Privacy Example

class myClass(object):

def __init__(self,p1='firstParam',p2='secondParam'):

self.var1=p1

self.__var2=p2

myInst = myClass()

myInst.__dict__.items()

[('_myClass__var2', 'secondParam'), ('var1', 'firstParam')]