Lecture 17: Classes
(Chapter 15)
CS 1110 Introduction to Computing Using Python
[E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]
http://www.cs.cornell.edu/courses/cs1110/2018sp
Lecture 17: Classes (Chapter 15) CS 1110 Introduction to - - PowerPoint PPT Presentation
http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 17: Classes (Chapter 15) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White] Announcements Lab 9 is out
[E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]
http://www.cs.cornell.edu/courses/cs1110/2018sp
§ http://www.cs.cornell.edu/courses/cs1110/2014sp/lectures/index.php § https://www.cs.cornell.edu/courses/cs1110/2016fa/lectures/10-18- 16/modules/morefun.py
2
3
id1
list
4
id2
Point3
5
6
Description Invariant Short summary More detail Attribute list Attribute Name
0.. 999999999 means from 0 to 999999999, inclusive This is handy notation, not Python.
7
id8
lname 'Andersen' ssn boss 1234 None Worker
def __init__(self, last_name, ssn, boss): """Initializer: creates a Worker Has last_name, ssn, and boss Pre: last_name a string, ssn an int in range 0..999999999, and boss either a Worker or None. self.lname = last_name self.ssn = ssn self.boss = boss w = Worker('Andersen', 1234, None) # this is the call to the constructor # the constructor calls __init__
8
id8
lname 'Erik' ssn boss 1234 None Worker
two underscores
use self to assign attributes
§ Folder is initially empty
§ self = folder name = identifier § Other arguments passed in order § Executes commands in initializer
id8
lname 'Erik' ssn boss 1234 None Worker
9
10
class Student(): """Instance is a student taking python""" def __init__(self, name, NetID, is_auditing): """Initializer: instance with name, NetID, auditing status name: student's full name [str] NetID: student's NetID [str], 2-3 letters + 1-4 numbers is_auditing: whether student is auditing the class [bool]""" self.name = name self.NetID = NetID self.is_auditing = is_auditing
11
assert type(name) == str, "name should be type str" assert type(NetID) == str, "NetID should be type str" assert type(is_auditing) == bool, "is_auditing should be type bool"
12
class Student(): """Instance is a student taking python""” enrollment = 0 def __init__(self, name, NetID, is_auditing): """Initializer: instance with name, NetID, auditing status name: student's full name [str] NetID: student's NetID [str], 2-3 letters + 1-4 numbers is_auditing: whether student is auditing the class [bool]""" self.name = name self.NetID = NetID self.is_auditing = is_auditing Student.enrollment = Student.enrollment + 1
13
14
Student Jon Li name jl200 NetID True
is_auditing
Student Jill Ba name jb110 NetID False
is_auditing
2
enrollment
<function-name>(<arguments>) len(my_list)
<object-variable>.<function-call> my_list.count(7)
15
Student Jon Li name jl200 NetID True
is_auditing __init__(self, name, NetID, is_auditing)
Student 2
enrollment
keyword class
Beginning of a class definition Specification (similar to one for a function) to define methods to define class variables
16
class Student(): """Specification goes here.""” enrollment = 0 def __init__(self, last_name, ssn, boss): . . . <snip> . . .
__init__(self, name, NetID, is_auditing)
Student
enrollment Python creates after reading the class definition
class Student():
def __init__(self, name, NetID, is_auditing): self.name = name self.NetID = NetID self.is_auditing = is_auditing
Student.enrollment = Student.enrollment + 1
def greet(self): """Prints information about the Student to the screen""" print("Hi! My name is "+ self.name) print(”My NetID is "+ self.NetID) if self.is_auditing: print("I'm auditing the class")
17
__init__(self, …) greet(self)
Student
enrollment
s1 = Student(“Jon Li”, “jl200”, True) print(“current enrollment = “+str(Student.enrollment))
18
s1 = Student(“Jon Li”, “jl200”, True) # finds attribute in object folder print(s1.name) # finds attribute in class folder print(s1.enrollment) ß dangerous
19
Student Jon Li name jl200 NetID True
is_auditing __init__(self, name, NetID, is_auditing)
Student 1
enrollment
20
21
import cs1110 s1 = cs1110.Student(“Jon Li”, “jl200”, True) print(s1.enrollment) s2 = cs1110.Student(“Jill Bo”, “jb20”, False) print(s2.enrollment) s2.enrollment = 3 print(s1.enrollment) print(s2.enrollment) print(cs1110.Student.enrollment)
22
import cs1110 s1 = cs1110.Student(“Jon Li”, “jl200”, True) print(s1.enrollment) s2 = cs1110.Student(“Jill Bo”, “jb20”, False) print(s2.enrollment) s2.enrollment = 3 print(s1.enrollment) print(s2.enrollment) print(cs1110.Student.enrollment) What?!?&%$?? Look at that in the Python Tutor.
23
s1 = Student(“Jon Li”, “jl200”, True) s1.greet() TypeError: greet() takes 0 positional arguments but 1 was given
class Student(): """Prints information about the Student to the screen""" def greet(self): # forgotten by you or me print("Hi! My name is "+self.name) print(”My NetID is "+self.NetID) if self.is_auditing: print("I'm auditing the class")
24
s1 = Student(“Jon Li”, “jl200”, True) s1.greet() NameError: global name ‘is_auditing' is not defined
class Student(): """Prints information about the Student to the screen""" def greet(self): print("Hi! My name is "+self.name) print(”My NetID is "+self.NetID) if self.is_auditing: # forgot self! print("I'm auditing the class")
§ boss refers to a Worker object § Some workers have no boss § Or maybe not assigned yet
§ None: Lack of (folder) name § Will reassign the field later!
25
id8
lname 'Erik' ssn boss 1234 None Worker
§ Write as assignments to parameters in definition § Parameters with default values are optional
§ p = Point3() # (0,0,0) § p = Point3(1,2,3) # (1,2,3) § p = Point3(1,2) # (1,2,0) § p = Point3(y=3) # (0,3,0) § p = Point3(1,z=2) # (1,0,2)
class Point3(): """Instances are points in 3d space x: x coord [int] y: y coord [int] z: z coord [int] """ def __init__(self,x=0,y=0,z=0): """Initializer: makes a new Point Precondition: x,y,z are numbers""" self.x = x self.y = y self.z = z …
26