Top Banner
Section 6.1 CS 106 Victor Norman IQ Unknown
25

Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

Jan 19, 2016

Download

Documents

Marion Fletcher
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: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

Section 6.1

CS 106Victor Norman

IQ Unknown

Page 2: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

The Big Q

What do we get by being able to define a class?!

Or

Do we really need this?!

Page 3: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

Ancient History (last Tuesday)

• A card is a tuple with 2 parts, a suit (one of “s”, “d”, “c”, “h”) and a number (2 – 14).

• We create a card by making a tuple.• We access the suit via card[0] and numbervia

card[1].• What is good and what is bad about this

implementation?

Page 4: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

What types of variables can we make?

• Is this good enough?

Page 5: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

Second big Q

What defines a type?

• Data + operations– what you can store.– what you can do to or with it.

Page 6: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

Lecture...

• a class definition is like a recipe (or template).– you don't eat the recipe, right?

• an object is an instantiation of that class – that's what you eat.

• Or, a class is a new type.

Page 7: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

Class (or Type)

• Marriage of data and methods.• Boundary between caller and implementer– perspective on the world.

• self is a reference to the object, from within the class code.

• Each object has its own namespace, accessible via self. (In fact, isn't an object just a namespace? Could be...)

Page 8: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

Creating class instances

Q: One creates a new instance of a class by calling the class ______________.

A: constructor

Page 9: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

What does constructor code do?

Q: When code instantiates a class, the __init__ method is called. This method's "primary purpose is to establish initial values for the ____________ of the newly created object."

A: attributes

Page 10: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

Code for Car constructor

Q: I have a class Car that I instantiate this way: car = Car()

Write the method definition for Car’s constructor.

A: def __init__(self):# code here to initialize

attributes to # default values … self._color = “black”

Page 11: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

“Setter” Signature

Q: After creating a Car instance (i.e., object), my main code wants to set the Car's color to a new color. Write the signature of the Car member function, setColor(), to set the car's color.

A: def setColor(self, color):

‘’’set the color of this car to the given color’’’

self._color = color

Page 12: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

“Getter” Signature

Q: Then, my main code wants to get the color from my car instance. Write the signature for the member function getColor().

A: def getColor(self):

‘’’return the color of this Car to the caller.’’’

return self._color # my color

Page 13: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

Syntax errors?

How many syntax errors are there in this code?:class Car def _init_(make, model, year): self.myMake = make self.myModel = model myYear = year

Page 14: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

Answer me this.Given this class definition:class Car: def __init__(self, make, model, year): self.myMake = make self.myModel = model self.myYear = yearwhich is legal code to make a new Car instance?A. aCar = Car.__init__(self, “Honda”, “Odyssey”,

2001)B. aCar = Car.__init__("Honda", "Odyssey", 2001)C. aCar = Car("Honda", "Odyssey", 2001)D. aCar = Car(self, "Honda", "Odyssey", 2001)E. Car(aCar, "Honda", "Odyssey", 2001)

Page 15: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

Why use classes?

What are the advantages/disadvantages of defining a class/type?Advantages:• Can control the state of each object

– the setter methods allow only some attributes to be changed.– Sets up a sort-of “firewall” around each object.

• Can control what a user can do with an object.• Is more “polite”: code “asks” an object to do something for it.• Other advantages: subclassing, etc.Disadvantages:• More syntax. More code.

Page 16: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

Accessors and no mutators?

Q: What would be the advantage to only giving the user accessor functions and no mutator functions?

A: You can restrict what attributes can be changed – i.e., you can make themimmutable

Page 17: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

Exampleclass Student: def __init__(self, name, id, grades): self._name = name self._id = id self._grades = grades def setName(self, newName): “””Change the student’s name””” self._name = newName # getName(), getId(), and getGrades() here.

# No setId() here: a student’s id can never # be changed after the object has been created.

Page 18: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

self

• Note that there is code “on the inside” – code inside the class definition.

• Code in __init__, getName(), etc.• This code refers to the object as self.• Then, there is code on the outside:stud1 = Student( “Dan”, 1040471, [100, 90, 80] )

• This code refers to the object as stud1.

Page 19: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

Exampleclass Student: def __init__(self, name, id, grades): self._name = name self._id = id self._grades = grades def getName(self): return self._name def setName(self, newName): self._name = newName def getId(self): return self._id# Create a studentstudent1 = Student( “Angelina”, 10, [] )

Page 20: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

Designing a Class

When you design a class, you decide:• what are the important properties (or attributes,

characteristics, or state) of an instance.• what information a caller should be able to get

from an object• what state a caller should be able to change in an

object• what a caller should be able to ask an object to

do to itself.

Page 21: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

What goes in a class definition?

Q: So it is my understanding that a class definition is a series of function definitions nested within the class definition… Is there anything else that goes into it?

A: No. The function definitions in the class definition define the operations that the objects provide. The attributes for each object are set in the constructor method.

Page 22: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

Naming Conventions

• A class name always starts with a capital letter. Each word in the class starts with a capital letter.– class MyFathersCar:– class Cs106Lab:

• A class attribute always starts with an underscore: _– self._x_loc, self._y_loc, self._color

Page 23: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

Naming Conventions

• local variables must start with a lowercase letter.• global variables must also start with a lowercase

letter, unless they are CONSTANTS, which must be all uppercase.

• function names start with a lowercase letter.• Either use camelCase for function names and

variables or use _'s between words in a name.– myXLocation, or, my_x_location– getLocation(), or, get_location()

Page 24: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

Order of methods in a class

Q: Does the order the methods are defined in a class matter?A: No. By convention the constructor method is always first, however.

Page 25: Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!

Attribute names

Q: Why would the coder choose _x and _y instead of x and y?A: It is a convention to name your attributes with names starting with _. The reader of the code can then know what something is when it sees the _ in front of the name.