Let’s get the obvious question out of the way. “This looks super fancy. Will I ever need this in real life?”
Yes. If you are using Python to connect to a SQL server, to download/insert some data. Easiest way to handle all that headache is to group all the fns together into a class. So, yes. Also, your data engineer (if your team has one), will toss the file which has the classes in your direction.
There are many other scenarios that classes are used in, but the above is what you’ll use them for, most often.
Table of Contents
Classes
Instances
Object Oriented Programming
1 - Classes
1.1 Defining a Class
Simple data structures are designed to represent simple pieces of information such as:
integers, and floats
strings
lists or tuples
But, let’s say we are interested in designing a data structure. Lets say that this structure can look, and track several important pieces of an object/person. In this case, we will want to use a new data type in Python called a class.
Classes are like blueprints for an object. When we actually create the physical object, that is called instantiating an object.
1.2 Creating a Simple Class
All classes built in Python use the class keyword. This is followed by the name of the class you wish to build, and a colon. Here’s an example of a class called BowTied.
class BowTied:
pass
In this case, you’ll see I gave my above class 1 simple keyword called pass. Pass is used as a simple placeholder to tell Python this is unfinished, I’ll get back to it later.
1.3 Class Attributes
When we make a class, we will want to give this class some attributes. Class attributes are used to store information that is shared by all instances of the class. For example, you might define a class attribute to store a particular number or string. In this example, we’ll say that every single BowTied class has a attribute called Twitter, and we’ll set this to True. We’ll also make another attribute called favorite number, and set it to 69
class BowTied:
Twitter=True
favorite_number=69
1.4 Instance Attributes
Sometimes when we make a class, and we use the class as a blueprint to make an object. Sometimes, we don’t want every single object to have the exact same attributes. Sometimes, we want objects to have different attributes when initialized.
In this case, we will use the __init__() method. init is short for the initial state of the object. In this example below, we’ll now say that every BowTied class has a different link to their substack. This link which must be defined when making an object using this class. We’ll also make another instance attribute called maturity level. This must also be defined by the user.
class BowTied:
Twitter=True
favorite_number=69
def __init__(self,substack,maturity):
self.substack=substack
self.maturity=maturity
If this sounds a bit confusing, don’t worry, when we make the instances using the classes in the below section, things will start to make much more sense.
2 - Instances
2.1 Instantiating an Object
When we use a class to create an object in Python, we call that instantiating an object. We will use the code created in 1.2 to create a simple object based off the BowTied class.
To create an object, we’ll put brackets after pointing to our class. To track it, we’ll also want to save the object to a variable. That is:
class BowTied:
pass
Raptor=BowTied()
print(Raptor)
If you run the above code, you’ll probably get something that looks like this:
So, wtf is that gibberish? Let’s talk about it.
2.2 Wtf is That Gibberish?
The above gibberish says that in the main python file, you have an object that is being created based off the BowTied class. This object is stored at 0x7fb4548a84c0. This goofy string of letters and numbers is referred to as the memory address for this object. This address tells us where this object is stored in our computer’s memory (RAM).
Your computer is different than mine, so you’ll get a different phrase instead.
2.3 Checking For “Sameness”
In Python, if we have a class. And we build 2 objects from that class. The two objects could have the exact same attributes, but they would be stored in different locations in the memory. Due to this, those two objects would be treated as different, and not the same.
The only other way to do this is to check if every single attribute is the exact same in the two classes. Next up, we’ll talk about looking at the attributes for your object.
2.4 Examining Class Attributes
Once we’ve made an object, we can use a dot (.) to look at the attribute for a class. Look at the class we made in 1.3, we’ll be using that one here.
class BowTied:
Twitter=True
favorite_number=69
Raptor=BowTied()
print('This is Raptor favorite_number:')
print(Raptor.favorite_number)
print('This is Raptor Twitter attribute')
print(Raptor.Twitter)
And here is the output
Once the class is made, it will inherit all the class attributes from the class without having it be specified. But, what about the instance attributes? How do we pass that off?
2.5 Examining Instance Attributes
Now, let’s talk about building objects using classes. This time, we’ll pass the object some common attributes, and some instance attributes. Look at the class we built in 1.4
In this example, we’ll pass a string as a substack value, and a maturity value.
class BowTied:
Twitter=True
favorite_number=69
def __init__(self,substack,maturity):
self.substack=substack
self.maturity=maturity
Raptor=BowTied(substack='bowtiedraptor.substack',maturity='zoomer')
Devil=BowTied(substack='degencode.substack',maturity='boomer')
You can use a simple print statement to see the attributes of these classes manually.
Observe:
With the basics on classes, and instances out of the way, let’s talk about why Python is considered an Object Oriented Programming Language.
3 - Object Oriented Programming (OOP)
3.1 What is OOP
Object-oriented programming is a style of programming that uses objects to model the real world. In object-oriented programming, each object has its own state and behavior, and the interaction between objects is controlled by their messages.
Here’s a vid that explains this:
3.2 Why is Python an OOP
Python is an OOP language because it provides many features that make it easy to create and use objects. Python allows you to create classes and instances. Then, once created it provides built-in support for method calls to access attributes.
Python also supports inheritance. This means that you can create classes that inherit the attributes and methods of other classes. This makes it easy to create complex objects by building on top of existing classes. Python supports polymorphism, which means that you can call the same method on different types of objects.
Click here to go to object inheritance in the next Python post.
Perfect explanation 🔥Great vid choice to explain the four key principles of OOP. Concepts on lock in my brain. Thank you Sir Raptor
This stuff is mind bending o_O will need to revisit