OOP in Python
July 31st, 2018
Computing in the News
Facebook announces it will shut off user data access for hundreds of thousands of apps until they show they are compliant with new data privacy regulations.
https://www.theverge.com/2018/7/31/17637244/facebook-apps-api-access-shut-off-missed-review-deadline
Announcements
Today’s Lecture: An Overview
OOP: A Review
Intuition
Class: Sprites
Instance: Alonzo sprite
Classes in Python
Classes and Instances
Classes and Instances
class Sprite:
size = ‘100’
def __init__(self, name,
color):
self.name = name
self.color = color
def say(self, text):
print(text)
Can you name each of the elements (class statement, constructor, and class method) in this code?
Classes and Instances
class Sprite:
size = ‘100’
def __init__(self, name,
color):
self.name = name
self.color = color
def say(self, text):
print(text)
Class Declaration
Object Constructor
Class Method
Class vs. Instance Attributes and Methods
Classes and Instances
class Sprite:
size = ‘100’
def __init__(self, name,
color):
self.name = name
self.color = color
def say(self, text):
print(text)
Which attributes are class attributes and which attributes are instance attributes?
Classes and Instances
class Sprite:
size = ‘100’
def __init__(self, name,
color):
self.name = name
self.color = color
def say(self, text):
print(text)
Class Attribute
Instance Attribute
Instantiating an Instance
Alonzo = Sprite.init(‘Alonzo’, ‘yellow’)
Accessing Object Data
>>> Alonzo.color
‘Alonzo’
>>> Alonzo.size
‘100’
>>> Alonzo.say(“CS10 WOO!”)
‘CS10 WOO!’
Mutating Your Objects
>>> NewSprite = Sprite.init(‘Alonzo’, ‘green’)
>>> NewSprite.color
>>> “green”
>>> NewSprite.color = “purple”
>>> NewSprite.color
>>> “purple”
Mutating Your Objects
>>> Alonzo.size = ‘1000000’
>>> NewSprite.size
‘100’
>>> Sprite.size = ‘1000’
>>> NewSprite.size
‘1000’
>>> Alonzo.size
‘1000000’
Check Your Understanding
If we didn’t change Alonzo’s size attribute first, what would Alonzo’s size be after we changed the Sprite class’s size attribute?
�
Check Your Understanding
If we didn’t change Alonzo’s size attribute first, what would Alonzo’s size be after we changed the Sprite class’s size attribute?
When we mutated the Alonzo object, we gave Python a local value to go to instead of a class value
What happened to the self?
class Sprite:
size = ‘100’
def __init__(self, name,
color):
name = name
color = color
def say(self, text):
print(text)
What happened to the self input?
Bound Methods
>>> NewSprite.say(“I love CS10!”)
>>> Sprite.say(NewSprite, “I love CS10!”)
Inheritance and Subclasses
When do we want subclasses?
Review: Subclasses and Inheritance
Subclasses
class Alonzo(Sprite):
color = ‘yellow’
antenna = True
def __init__(self, name, color):
self.name = name
def say(self, text):
print(“CS10 WOO!”)
Inheritance: Selective Sharing
Checking Your Understanding: WWPP
What would Python print in the following script?
>>> SpriteOne = Sprite(‘One’, ‘green’)
>>> SpriteTwo = Alonzo(‘Alonzo’, ‘blue’)
>>> SpriteTwo.say(‘It’s a nice day out’)
Checking Your Understanding: WWPP
What would Python print in the following script?
>>> SpriteOne = Sprite(‘One’, ‘green’)
>>> SpriteTwo = Alonzo(‘Two’, ‘blue’)
>>> SpriteTwo.say(‘It’s a nice day out’)
Checking Your Understanding: WWPP
What would Python print in the following script?
>>> SpriteOne = Sprite(‘One’, ‘green’)
>>> SpriteTwo = Alonzo(‘Two’, ‘blue’)
>>> SpriteTwo.say(‘It’s a nice day out’)
Checking Your Understanding: WWPP
What would Python print in the following script?
>>> SpriteOne = Sprite(‘One’, ‘green’)
>>> SpriteTwo = Alonzo(‘Two’, ‘blue’)
>>> SpriteTwo.say(‘It’s a nice day out’)
What would SpriteOne.say(‘It’s a nice day out’) print?
‘It’s a nice day out’
Checking Your Understanding: WWPP
What would Python print in the following script?
>>> SpriteOne = Sprite(‘One’, ‘green’)
>>> SpriteTwo = Alonzo(‘Two’, ‘blue’)
>>> SpriteTwo.size
Checking Your Understanding: WWPP
What would Python print in the following script?
Would this print the same result as SpriteOne.size? Yes / No
>>> SpriteOne = Sprite(‘One’, ‘green’)
>>> SpriteTwo = Alonzo(‘Two’, ‘blue’)
>>> SpriteTwo.size
Checking Your Understanding: WWPP
Why would the first result give different outputs, but the second result gives the same output?
Dot Expression Rules
<expression>.<name>
How to evaluate:
Dot Expression Rules
>>> lonzo = Alonzo(‘Lonzo’, ‘purple’)
>>> lonzo.name # found in instance
‘Lonzo’
>>> lonzo.size # found in Sprite
‘100’ superclass
>>> lonzo.color # found in Alonzo
‘yellow’ class
Inheritance: Selective Sharing
Subclass Practice
Subclass: Alonzo
Alonzo subclass: Terminalonzo
Let’s Design Terminalonzo
class Terminalonzo(_______):
________________
def __init__(self, name, color):
__________________
def say(self, text):
__________________
Let’s Design Terminalonzo
class Terminalonzo(Alonzo):
color = ‘grey’
def __init__(self, name, color):
self.name = “T-” + name
def say(self, text):
print(“I’LL BE BACK”)
Summary