Introduction to Classes
Data Programming
1
Sli.do
#cse160
Icebreaker
What is your favorite pokemon?
Questions?
CSE 160
CSE 160: Introduction to Classes
Announcements
2
CSE 160: Introduction to Classes
Today’s Roadmap
3
CSE 160: Introduction to Classes
So Far in CSE 160…
4
1
2
3
4
5
How can we (further) organize all of this?
CSE 160: Introduction to Classes
Objects
(Real-world) objects have attributes and can (sometimes) perform actions
5
Attributes
Actions
CSE 160: Introduction to Classes
Python Objects
Python objects bundle data and functionality together.
6
What if built-in data structures don’t suffice? �→ Let’s create our own!
my_keurig.brew("French Vanilla")
CSE 160: Introduction to Classes
Classes
Classes provide the blueprint for creating an object
7
Class Blueprint
Specific Object
.brew(...)
CSE 160: Introduction to Classes
Classes in Python
8
class ClassName:
class_var = 42�� def my_method(self, parameter):� � self.instance_var = parameter�� # ...
class keyword
class name (uppercase)
everything is indented!
function defined inside of class definition
A class variable is an attribute of the class
this is called a method
this is new… (more on this soon!)
CSE 160: Introduction to Classes
Creating an Object
9
class ClassName:�� def __init__(self, parameter):� � self.parameter = parameter
my_object = ClassName(18)
defining the class (template)
creating an instance of the class (i.e. an object)
special method that is called when the object is created
self is a variable that refers to the object itself
An instance variable is accessible only to the object (instance)
not the same variable!
CSE 160: Introduction to Classes
Questions?
Live and on Sli.do
10
Sli.do
#cse160
CSE 160: Introduction to Classes
Example: Pokemon
11
class Pokemon:
hp = 20�� def __init__(self, name, kind):� � self.name = name
self.type = kind
Charmander
Fire Type Pokemon
charmander = Pokemon("Charmander", "Fire")
CSE 160: Introduction to Classes
Think Pair Share
Consider these two instances of the Pokemon class:
12
A
B
C
D
E
Sli.do
#cse160
charmander = Pokemon("Charmander", "Fire")
piplup = Pokemon("Piplup", "Water")
charmander.evolve()
�print(piplup.hp)
What is printed when these two lines of code are run?
CSE 160: Introduction to Classes
CSE 160: Introduction to Classes
Using an Object
13
class ClassName:�
class_var = 42
� def __init__(self, parameter):� � self.parameter = parameter
my_object = ClassName(18)
creating an instance of the class (i.e. an object)
my_object.parameter
my_object.class_var
ClassName.class_var
18
42
42
CSE 160: Introduction to Classes
Example: Universities
What attributes and functionality does a university have?
14
University of Washington
CSE 160: Introduction to Classes
Think Pair Share
15
Create a class called University with the following attributes and methods:
"[mascot] welcomes you to [name]"
University of Washington
CSE 160: Introduction to Classes
CSE 160: Introduction to Classes
Questions?
Live and on Sli.do
16
Sli.do
#cse160
CSE 160: Introduction to Classes
Anatomy of a Class
17
class ClassName:
class_var = 42�� def __init__(self, parameter):� � self.instance_var = parameter�� # ...
class keyword
class name (uppercase)
everything is indented!
special method that is called when object is created
A class variable is an attribute of the class (shared across instances)
variable refers to the object itself
The value of an instance variable is unique to the instance of the class
CSE 160: Introduction to Classes