Skip to content

Instantly share code, notes, and snippets.

@yelluw
Last active May 13, 2017 22:15
Show Gist options
  • Save yelluw/0c85580d285c84e9e73bfe266bd17024 to your computer and use it in GitHub Desktop.
Save yelluw/0c85580d285c84e9e73bfe266bd17024 to your computer and use it in GitHub Desktop.
Intro to Python classes for you.
"""
Dude,
A Python class is something that defines a thing.
Huh?
Let me explain.
Say we are told to create an app about beds. Yes, beds.
Computers don't know what beds are (otherwise they'd be sleeping all day).
It is our job to tell them what a bed is and how it works.
That's where classes come in.
A class is simply the blueprints or instructions of something.
It comes from what is known as object oriented programming (OOP).
OOP tries to make programming simpler by allowing us to define things in code.
An object is a thing. You can think of OOP as Thing Oriented Programming. Or in
simpler terms, programming with things.
How about that bed? How could we write a bed class?
Before that, let me clarify something. A class is the definition of an object (thing).
By writing a bed class, we are pretty much defining what a bed is and how it works with code.
"""
"""
Python makes writing classes simple. It requires a very minimal amount of code to define a class.
See the example below:
"""
class Bed(object):
pass
"""
That's it!
Note that the word object follows the word Bed.
This tells Python that the Bed is related to an object.
There is more to it, but I'm keeing it simple now.
The word pass simply states that this class is empty.
There is nothing else going in there.
"""
"""
Objects need to be instantiated. This is all fancy talk for created.
When we create something, we need to know its features.
We can't just make a bed!
We need to know the size of the bed (king, queen, full, twin).
We need to know the height of the bed.
We need to know the color as well.
We need to know handful of thnings before we set off to create one.
In the same way that a carpenter needs to know the measurements of a table before cutting the wood.
"""
"""
Python provides an easy way to instantiate those damn objects. Here is how:
"""
class Bed(object):
def __init__(self):
pass
"""
OK. A couple of things there.
First. Classes can have their own functions. This allow us to define addittional functionality
for whatever we may need. In the case of classes, functions are called methods. Don't ask why.
Just remember that when I say methods, I'm referring to a function that belongs to a class.
Second. The word __init__ is confusing. That's a naming convention in Python.
The method that instantiates objects is called the __init__ method.
Don't ask why.
Third. The word self is confusing. It means that whenever we do something with this object it will
need to reference itself. Why? You might have 5 Bed objects. Python needs to know which is which.
self is a way that tells them apart.
Fourth. Note that the method __init__ is 4 spaces inside the class. This is necessary.
It means that the method belongs to the class. Needs to be inside the class in order to belong.
Like a little kangaroo inside its mother's pouch.
Every method belonging to a class must be 4 spaces in. Otherwise it's not part of the class.
"""
"""
OK, let's add another method to the class.
"""
class Bed(object):
def __init__(self):
pass
def make_bed(self):
return "bed is now made"
"""
I added a method called make_bed.
Whenever we use that method the bed will be made!
I wish I had that method in real life. :D
"""
"""
Let's now create a bed and make it.
"""
bed = Bed()
bed.make_bed()
"""
We first create the bed object using
the Bed class.
Then we call the method make_bed to make the bed.
"""
"""
Use this to improve your current code.
See what's missing.
Ask any questions in the comments below or email me pryelluw@gmail.com
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment