A memorandum for myself. Focused on understanding at the cost of accuracy.
class is
A blue print for creating objects.
Introduction
As introduced in the previous post, object is a package of methods and data. class is a blue print or template to create those objects. Every time you call a class, you will create a new object (which is called Instance.
Example
# declare that this is a blue print of "glass_of_water"
class glass_of_water:
# first, you have water as your data
my_data = 'water'
# this is called attribute
# then, define the method
def pour(self):
# this is the actual action of the method
print('pour ' + self.my_data)
Let's try to run this code.
First, create an object with the blue print of glass_of_water.
object1 = glass_of_water()
Now you run it.
>>> object1
<__main__.glass_of_water at 0x7f7fb0a76198>
You get nothing because you didn't specify what to do, i.e. the method.
Let's do it again.
>>> object1.pour()
pour water
Reference
https://www.hackerearth.com/practice/python/object-oriented-programming/classes-and-objects-i/tutorial/
https://qiita.com/lamplus/items/351145a81f798ecec80b
https://qiita.com/hiroyuki_mrp/items/140d47df01553091d59a