1 of 17

Introduction to Classes

Data Programming

1

Sli.do

#cse160

Icebreaker

What is your favorite pokemon?

Questions?

CSE 160

CSE 160: Introduction to Classes

2 of 17

Announcements

  • Programming Practice 7 due Wednesday, February 25 at 11:59 PM
  • Homework Assignment 4 due Thursday, February 26 at 11:59 PM
  • Resubmission Cycle 4 opens today
    • Only three more resubmission cycles! (including this one)

2

CSE 160: Introduction to Classes

3 of 17

Today’s Roadmap

  1. Organizing Code
  2. Objects (Real and Pythonic)
  3. Classes in Python
  4. Examples

3

CSE 160: Introduction to Classes

4 of 17

So Far in CSE 160…

  • Variables, Expressions, Data Types�
  • Loops�
  • Conditionals�Functions�Data Structures

4

1

2

3

4

5

How can we (further) organize all of this?

CSE 160: Introduction to Classes

5 of 17

Objects

(Real-world) objects have attributes and can (sometimes) perform actions

5

Attributes

Actions

  • Color
  • Cost
  • Keurig Model
  • Type of K-Cup
  • Brew Time
  • Brew Coffee�(Input: K-Cup)
  • Descale

CSE 160: Introduction to Classes

6 of 17

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

7 of 17

Classes

Classes provide the blueprint for creating an object

7

Class Blueprint

Specific Object

  • color
  • model
  • brew_time
  • color = "Glamping Green"
  • model = "K-Mini Mate"
  • brew_time = 30

.brew(...)

CSE 160: Introduction to Classes

8 of 17

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

9 of 17

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

10 of 17

Questions?

Live and on Sli.do

10

Sli.do

#cse160

CSE 160: Introduction to Classes

11 of 17

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

12 of 17

Think Pair Share

  • 20

  • 40

  • 60

  • 80

  • Error

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

13 of 17

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

14 of 17

Example: Universities

What attributes and functionality does a university have?

14

University of Washington

CSE 160: Introduction to Classes

15 of 17

Think Pair Share

15

Create a class called University with the following attributes and methods:

  • name, a string
  • mascot, a string
  • enrollment, an int
  • depts, a list of strings

  • welcome(), that prints:

"[mascot] welcomes you to [name]"

  • add_dept(new_dept), that adds new_dept to the depts list

University of Washington

CSE 160: Introduction to Classes

CSE 160: Introduction to Classes

16 of 17

Questions?

Live and on Sli.do

16

Sli.do

#cse160

CSE 160: Introduction to Classes

17 of 17

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