Some notes from exercises 40, 41 & 42 in Learn Python the Hard Way. The video for exercise 40 is where Zed reveals how he hasn’t yet taught the most important thing in Python, which was actually cool for me to hear right now. I mean… no wonder I can’t build a game yet! 😜
Thanks to Nicole Sullivan, I’m used to thinking about OOCSS. I have no idea how or if this compares to OOP in any way that is useful for me, but that I am about to find out. Zed explains that OOP is “a convention to make components that can talk to each other”. Also why you don’t just use modules:
You can take this MyStuff class and use it to craft many of them, millions at a time if you want, and each one won't interfere with each other.
Intro to some words and concepts 🌱
classis a container for a grouping that I can use as a blueprint
objectis what I get when I use a class to create a new instance of it
instantiateis what I’ve done when I’ve used a class to create an object
selfis needed because in OOP I need a reference to what I am are working on
__init__will initialize my newly created empty object
Get a thing from another thing
There’s a pattern to recognize here on how to get things from other things.
fromthis['wantthis'] # dictionary key=value
modulewhatever.somethingelse() # module key=value
blueprint.anotherthing # also a key=value when I have a class
OOP and FP
I found an article on CodeNewbie with helpful explanations like:
In all programs, there are two primary components: the data (the stuff a program knows) and the behaviors (the stuff a program can do to/with that data). OOP says that bringing together data and its associated behavior in a single location (called an “object”) makes it easier to understand how a program works. FP says that data and behavior are distinctively different things and should be kept separate for clarity.
__init__ can be pronounced “dunder init dunder”. 💜
(About the origin: wiki.python.org)
Exercise 41 has a script to help drill these phrases. You run it in the terminal to repeat either from code to English, or the other way around. With different random words from a list each time.
# Make a class named Dinosaur that is-a Cactus
# class Brick has-a __init__ that takes self and cherry params
def __init__(self, cherry)
# class Cabbage has-a function named Book that takes self and drum params
def Book(self, drum)
# Set actor to an instance of class Celery
actor = Celery()
# From dust, get the crayon function, and call it with params self, detail
# From approval, get the brain attribute, and set it to Coal
approval.brain = Coal
Random nonsense words in this script was super helpful. I’ve seen this a lot from both learning and teaching — that as a beginner, you get tripped up on which words are from the language, and which are just something we name as we code. So when teaching non-coders their very first HTML/CSS, I now use obviously random silly words to take the focus away from class naming.
Exercise 42 starts to introduce these concepts:
a class can inherit traits from another class | is-a | | composition
a class can be composed of other classes as parts | has-a |
You use the phrase is-a when you talk about objects and classes being related to each other by a class relationship. You use has-a when you talk about objects and classes that are related only because they reference each other.