by Albert Major
Object orientation is a programming approach where you group together data (called attributes) and the actions that operate on that data (called methods) into self-contained “objects,” making code easier to manage and closer to real-world concepts.
class Student, class Point, etc.)Student or Point with actual values set for all the attributes.)student_number, x/y coordinates, etc.self as the first argument).Point Class
Defining a Class:
class Point:
pass
pass is a placeholder meaning “do nothing.”Instantiating an Object:
p1 = Point()
print(p1) # Shows something like <__main__.Point object at 0x...>
print(type(p1)) # Shows <class '__main__.Point'>
Adding Attributes With __init__:
__init__ is the constructor. It automatically runs when you create a new object.
It always has self as the first parameter.
You can specify other parameters and assign them to self.attribute_name:
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
Now you can do:
p1 = Point(5, 6)
print(p1.x, p1.y) # 5 6
If no arguments are passed, it defaults to (0, 0).
__repr__ and __str__
By default, print(object) shows <__main__.Point object at some_memory_address>.
You can override the special methods:
__repr__(self): Returns a complete string representation of the object.__str__(self): Returns a string version usually meant for the end-user (often simpler).In Python, if you only implement one of them, Python tries to use it for both str() and repr().
Example using __repr__:
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __repr__(self):
return f"({self.x},{self.y})"
Now print(Point(5, 6)) will display (5,6).
Methods are just functions inside a class. They always have self as the first parameter.
Example: distance from origin
import math
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def distance(self):
return math.sqrt(self.x * self.x + self.y * self.y)
Calling it:
p1 = Point(5, 6)
print(p1.distance()) # calculates √(5² + 6²)
Example: translate (shift the point in x and y)
def translate(self, shift_x, shift_y):
self.x += shift_x
self.y += shift_y
self.x += shift_x modifies the object in-place. This makes Point a mutable class.random.shuffle(some_list) shuffles a list.shuffle method on your own Point class (though it might just swap x and y, for example).If one class stores a reference to an object of another class, it is called an association.
Example: a Rectangle class might hold a Point as its top-left corner:
class Rectangle:
def __init__(self, p, w, h):
self.p = p # p is a Point
self.w = w
self.h = h
If p changes (because you mutate the original point), the rectangle might also be affected—because it refers to the same point object.
If you don’t want a Rectangle to change when the original point changes, you can copy the point inside the constructor:
import copy
class Rectangle:
def __init__(self, p, w, h):
self.p = copy.copy(p) # or copy.deepcopy(p)
self.w = w
self.h = h
copy.copy() does a “shallow” copy (copies the top layer). copy.deepcopy() copies everything recursively.
Use deepcopy if the contained objects are complex (lists of lists, etc.).
Point objects in a loop but never store them in a list or variable for later use, they’ll be freed as Python goes.