Pretext: I am a Python beginner. Since I have no previous learning experience in programming languages, when I learned Python classes and objects, I struggled understanding it. The main idea seemed to make sense but I was lost in it all the same.
While in china visiting my wifes family, I found a blog (in chinese translated to english) by Long Ge who truly explained this in a way I finally understood. The author himself come to find out had watched a video by John Philip Jones , on YouTube and found a tutorial on Python classes and objects . Most of what I write is taking from these two people who helped me better understand OOP.
Lets have a look at the classes and objects. We will discuss the relationship between both. But before we get into Python classes and objects I want to use an analogy. Using the principles of building things in life. Weather it's a house, or a boat, or a cake they all need a plan, a blue print of sorts, a recipe.

-----A RECIPE (Set of Ingredients)-----------Manufacturing Process (set of Instructions)-------------------BluePrint (set of guidelines)-----
If you're going to build a home the first thing you need is a blueprint. From this blueprint you could construct your home.
Equally, if you were to build a cake the first thing you'd need is a recipe. And so a Class in Python is nothing more than a set of instructions.

You can see we've built a house from the blueprint, of course using the same blueprint you can build another house and another...
So these three houses all share something in common, they have all the same blueprint. (Separate Objects from the same Class)

A Class is like the blueprint while the Object is the house created from the Class or blueprint.
So, a constructed house is an Object. Something you can use and work with. In Python, ' everything is an object ', which is what everyone calls object oriented.
When we say in python:
>>> box = "apple"
Python notices a string " " and calls the str() Class to create this string Object.
>>> type(box)
<class 'str'>
When we use a function called type, Python tells us what class (box) belongs to. In this case it's a 'STR' string Class.
>>> box2 = "oranges"
>>> type(box2)
<class 'str'>
Look closely as we create a new box2, it's also made from the same blueprint or Class as box was. They are both 'STR' belongin to the string Class in Python.
Now you could say box & box2 are Objects right...? Kind of, because they are more like addresses pointing to the Object "apple" and "oranges".
Box and Box2 are Object Links or seperate house address containing the same blueprint or Class 'STR'.
>>> box = "apple"
>>> box2 = "oranges"
>>> f'TYPE:{type(box)} --> ID:{id(box)} --> VALUE:{box}'
'TYPE:<class 'str'> --> ID:62577760 --> VALUE:apple'
>>> f'TYPE:{type(box2)} --> ID:{id(box2)} --> VALUE:{box2}'
'TYPE:<class 'str'> --> ID:68662400 --> VALUE:oranges'
Let's review, when you type on the keyboard >>> box = "apple" and hit enter:
1. You awaken a String Class <class 'str'> inside Python.
2. Which then creates an Object ("apple", 'str' class, ID:68662400) linked to the variable box.
Got it!
In our analogy, it'll go a little like this.
When you (Bob) submit your home idea to a contruction company they give you an address for where your home will be built.
1. You begin construction
>>> bob = "home" # 'bob' here is used as a variable to link the actual important stuff your home.
2. The construction company uses a Blueprint Class to build your home.
3. This creates your Object (physical house, 'blueprint class', address: 1150 McLane dr. Los Angeles CA 98402) linked to your name bob.
Now if we change box2 to "apple" what will happen:
>>> box = "apple"
>>> box2 = "apple"
>>> f'TYPE:{type(box)} --> ID:{id(box)} --> VALUE:{box}'
'TYPE:<class 'str'> --> ID:62577760 --> VALUE:apple'
>>> f'TYPE:{type(box2)} --> ID:{id(box2)} --> VALUE:{box2}'
'TYPE:<class 'str'> --> ID:62577760 --> VALUE:apple'
Python doesn't need to create a whole new Object it just links box2 to the Object already created which match your request.
>>> box == box2
True
Now box and box2 are literally the same Object, they have the same ID address, Class, and Value.
Let's not think anymore of placing "apple" in a box but move on to a greater understanding of what is actually happening. The Object that is made and the variable that is used to link to that Object.
Think how cool this is... knowing the true order of things.
class Bank:
def __init__(self, balance, name):
self.balance = balance
self.name = name
def savings(self, deposit):
self.balance += deposit
return f'${self.balance}'
def credit(self, withdrawal):
self.balance -= withdrawal
return f'${self.balance}'
def __str__(self):
return f'Balance: ${self.balance} ' \
f'Name: {self.name}'
user1 = Bank(150, "Davi")
print(user1)
user1.savings(200)
print(user1)
class Bank:
def __init__(self, balance, name):
self.balance = balance
self.name = name
This __init__ is a command that initializes everytime automatically whenever that class is called.
In our case we want to init a balance variable and a name variable. We will go into more depth later on self.balance and self.name
Copy and Paste this example and being playing around with it. Explore and you'll start to understand all of these things better.
Here is the tutorial that taught me pretty much all of this and more. :) Enjoy