19 Classes
19 Classes
CS106AP Lecture 19
Roadmap sics
am m i ng Ba
Progr The C
onsol
e Ima
ges
Day 1!
Object-Oriented
Everyday Python
Programming
Object-Oriented
Everyday Python
Programming
Pa Pa Pa
rt rt rt
1 2 3 Life after CS106AP!
How do we animate our graphics
Today’s programs?
topics 3. Classes
4. What’s next?
Review
campy
New graphics library: campy
● Python version of Stanford’s ACM Graphics Libraries
height
return rect
width
GOval
def get_oval():
oval = GOval(width=100, height=50, x=0, y=0)
oval.filled = True
oval.fill_color = ‘green’ (x, y)
height
return oval
width
GLine
line = GLine(x0, y0, x1, y1)
# similar to tk’s create_line() function!
(x, y)
hi
Randomness
The random module
● random is a useful Python module
import random
COLORS = ['red', 'orange', 'yellow', 'green', 'blue']
color = random.choice(COLORS)
The random module
● random is a useful Python module
import random
COLORS = ['red', 'orange', 'yellow', 'green', 'blue']
color = random.choice(COLORS)
randomly selects
from a list
The random module
● Generate a random float 0 <= x < 1
random.random()
random.choice(lst)
Random bubbles:
draw_bubbles()
[demo]
How do we animate our
graphics programs?
Random bubble:
animate_bubble_pop()
[demo]
Use an animation loop
while True:
if stop_condition:
break
# ‘Animate’ object
if stop_condition:
break
# ‘Animate’ object
if stop_condition:
break
# ‘Animate’ object
pause(timestep)
How long to wait in
milliseconds
Random bubble:
animate_bubble_pop()
Random bubbles:
animate_many_bubbles()
Random bubbles:
animate_many_bubbles()
[demo]
path to feature(s) we
specific module want to use
Using modules - two ways
1. Import the module 1. Import specific feature from module
import module from module import function
2. Use the predefined features! 2. Use the feature!
module.function() function()
Method A Method B
Using modules - two ways
1. Import the module
import campy.graphics.gobjects
2. Use the predefined features!
campy.graphics.gobjects.GOval(width, height)
Method A
Using modules - two ways
1. Import the feature from module
from campy.graphics.gobjects import GOval, GRect
2. Use the feature!
GOval(width, height)
Method B
Using modules - two ways
1. Import the module 1. Import specific feature from module
import module from module import function
2. Use the predefined features! 2. Use the feature!
module.function() function()
Method A Method B
more concise, especially if you’re
only importing 1-2 things
What are we importing from the module?
from random import randint
A function!
from campy.graphics.gobjects import GOval, GRect
What are we importing from the module?
from random import randint
i.e. “classes”
How and why would we define
our own data types?
How and why would we define
our own data types?
How and why would we define
our own data types?
Classes!
Definition
class
A Python class defines a new data type for
our programs to use.
Definition
class
A Python class defines a new data type for
our programs to use.
● Pixel
Definition
instance
When we create an object that is our new type,
we call this creating an instance of our class.
What is a class?
● A blueprint for a new type of Python object!
○ The blueprint describes a general structure, and we can create
specific instances of our class using this structure.
● 3 main parts
○ Attributes
○ Methods
○ Constructor
What is a class?
● A blueprint for a new type of Python object!
○ The blueprint describes a general structure, and we can create
specific instances of our class.
● 3 main parts
○ Attributes (e.g. oval.fill_color, oval.width, etc.)
○ Methods
○ Constructor Variables stored inside
the class
What is a class?
● A blueprint for a new type of Python object!
○ The blueprint describes a general structure, and we can create
specific instances of our class using this structure.
● 3 main parts
○ Attributes (e.g. oval.fill_color, oval.width, etc.)
○ Methods (e.g. oval.move())
○ Constructor
Functions you can call on
the object
What is a class?
● A blueprint for a new type of Python object!
○ The blueprint describes a general structure, and we can create
specific instances of our class using this structure.
● 3 main parts
○ Attributes (e.g. oval.fill_color, oval.width, etc.)
○ Methods (e.g. oval.move())
○ Constructor (e.g. GOval(width, height))
Definition
instance attributes/instance variables
These variables belong to a specific instance of
our class, and every new instance of our class can
have its own values for each of them.
How do we design a class?
We must specify the 3 parts:
instance.attribute
How do we design a class?
We must specify the 3 parts:
image.width
How do we design a class?
We must specify the 3 parts:
Definition
methods
Methods are functions that belong to a class and
can be called on objects that are of the type the
class defines.
How do we design a class?
We must specify the 3 parts:
instance.method(args)
How do we design a class?
We must specify the 3 parts:
image.get_pixel(x, y)
How do we design a class?
We must specify the 3 parts:
instance = ClassName(args)
How do we design a class?
We must specify the 3 parts:
● Methods?
PynstaUser: We must specify our 3 parts
● Attributes
○ Name (string)
○ Posts (list of strings)
○ Friends (list of other PynstaUsers)
● Methods
○ Post a status
○ Add a friend
PynstaUser: We must specify our 3 parts
● Attributes
○ Name (string)
○ Posts (list of strings)
○ Friends (list of other PynstaUsers)
● Methods
○ Post a status
○ Add a friend
● Constructor?
PynstaUser: We must specify our 3 parts
● Attributes
○ Name (string)
○ Posts (list of strings)
○ Friends (list of other PynstaUsers)
● Methods
○ Post a status
○ Add a friend
● Methods
○ Post a status
○ Add a friend
● Constructor: PynstaUser(name)
Defining a class
class PynstaUser:
Defining a class
class PynstaUser:
Style note
class names
Uppercase the first letter of every word in class
names
Defining a class
class PynstaUser:
def __init__(self):
user = PynstaUser(‘Kylie’)
Defining a class
class PynstaUser:
user = PynstaUser(‘Kylie’)
Defining a class
class PynstaUser:
user = PynstaUser(‘Kylie’)
Defining a class
class PynstaUser:
user = PynstaUser(‘Kylie’)
Defining a class
class PynstaUser:
● For others
Why do we use classes?
● For ourselves
● For others
○ We hide the implementation details of our code so others don’t
need to worry about them.
○ They can just use the class, like we do for SimpleImage.
Why do we use classes?
● For ourselves
● For others
○ We hide the implementation details of our code so others don’t
need to worry about them.
○ They can just use the class, like we do for SimpleImage.
● For others
○ We hide the implementation details of our code so others don’t
need to worry about them.
○ They can just use the class, like we do for SimpleImage.
What’s next?
More on classes next week
● A better Pynstagram
○ Can friendships be bidirectional?
● Encapsulation
○ How classes help us
○ Classes + graphics programs
● Abstraction
○ How classes help people who use our code
Roadmap sics
am m i ng Ba
Progr The C
onsol
e Ima
ges
Day 1!