Tutorial: Understanding Classes, __init__, and the other.x Concept in Python
This tutorial explains step-by-step how Python classes work, how __init__ creates object attributes, and why calling a method like p1.distance_to(p2) allows us to use other.x.
What is self?
self is a reference to the object that is currently being used.
It means:
-
Inside a class method,
selflets the code access the object’s own data. -
Without
self, the object would have no memory, no attributes, and no way to store values.
Key idea
When you write:
Python internally does:
So inside __init__:
-
self=p1 -
x= 3 -
y= 4
Then you assign:
So self is literally the same object as p1.
Without self the object cannot store anything
When you call a method
If you do:
Python converts it to:
So inside the method:
-
self=p1 -
other=p2
That is why:
means → p1.x
and
means → p2.x
Summary (the essence)
self means:
1. Full Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import math class DataPoint: def __init__(self, x, y): self.x = x self.y = y def distance_to(self, other): dx = self.x - other.x dy = self.y - other.y return math.sqrt(dx*dx + dy*dy) p1 = DataPoint(0, 0) p2 = DataPoint(3, 4) d = p1.distance_to(p2) print("p1:", p1.x, p1.y) print("p2:", p2.x, p2.y) print("distance:", d) |
2. What a Class Is
class DataPoint: defines a new type of object.
Objects created from this class will have the structure and behavior we define inside it.
3. Understanding __init__
The constructor:
|
1 2 3 4 |
def __init__(self, x, y): self.x = x self.y = y |
This method runs automatically when you create an object:
|
1 2 |
p1 = DataPoint(0, 0) |
During this call:
- Python creates a new empty DataPoint object.
- Python calls:
12DataPoint.__init__(p1, 0, 0) - Inside the constructor:
self.x = xcreates the attributep1.xand sets it to 0.self.y = ycreates the attributep1.yand sets it to 0.
After creation:
|
1 2 3 |
p1.x == 0 p1.y == 0 |
The same happens for p2.
4. What Happens in p1.distance_to(p2)
The method:
|
1 2 3 4 5 |
def distance_to(self, other): dx = self.x - other.x dy = self.y - other.y return math.sqrt(dx*dx + dy*dy) |
When you call:
|
1 2 |
p1.distance_to(p2) |
Python transforms it internally into:
|
1 2 |
DataPoint.distance_to(p1, p2) |
So inside the method:
selfrefers top1otherrefers top2
This is why other.x works:
- because
otheris a DataPoint - and every DataPoint has
.xand.ycreated in__init__
Values in our example:
|
1 2 3 4 5 |
self.x = 0 self.y = 0 other.x = 3 other.y = 4 |
So:
|
1 2 3 4 |
dx = 0 - 3 = -3 dy = 0 - 4 = -4 distance = sqrt( (-3)^2 + (-4)^2 ) = 5 |
5. Debug Version to See Everything
You can paste this into WordPress to show internal behavior clearly.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import math class DataPoint: def __init__(self, x, y): print(">> __init__ called with:", x, y) self.x = x self.y = y print(" self.x =", self.x) print(" self.y =", self.y) def distance_to(self, other): print("\n>> distance_to called") print(" self =", self) print(" other =", other) print(" self.x =", self.x) print(" self.y =", self.y) print(" other.x =", other.x) print(" other.y =", other.y) dx = self.x - other.x dy = self.y - other.y print(" dx =", dx) print(" dy =", dy) dist = math.sqrt(dx*dx + dy*dy) print(" distance =", dist) return dist p1 = DataPoint(0, 0) p2 = DataPoint(3, 4) d = p1.distance_to(p2) print("\nFinal distance =", d) |
6. Connection to AI
This simple class is similar to how AI handles data:
- A DataPoint is like a sample with features.
- Distance calculation is used in algorithms like k-Nearest Neighbors, clustering, anomaly detection.
You can expand to more features:
|
1 2 3 4 5 6 7 8 9 10 |
class DataPointN: def __init__(self, features): self.features = features def distance_to(self, other): s = 0 for a, b in zip(self.features, other.features): s += (a - b)**2 return math.sqrt(s) |
This is the exact idea behind many classical machine-learning models.
If you want, I can prepare a second part of this tutorial:
- N-dimensional points
- A mini k-NN classifier
- A complete beginner AI exercise using only pure Python