15. Create Class, Object and Method with Examples in Python (Video in Khmer)
Learn to create and use classes, objects and methods in Python with simple examples. Perfect for beginners.
2026-10-25 ยท 3 min read
Hello friend! ๐ A class is like a plan. An object is made from that plan. Objects can have data and actions.
In this post, I show you how to make a class, create objects, and use methods. Easy and short. ๐
๐งฑ Create a Class and Object
Use class to make a class. Create an object by calling the class name with ().
class Person:
pass
person1 = Person()
print(person1) # A Person object
pass mean the class is empty for now.
๐ฅ Video: Introduction to Class and Object in Python (Khmer)
๐ฆ Add Data with __init__
__init__ run when you create an object. Use self to save data in that object.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
person1 = Person("Dara", 20)
print(person1.name) # Dara
print(person1.age) # 20
name and age are attributes. Each object can have its own values.
๐ฅ Video: Create Person Class and Access Attributes (Khmer)
โ๏ธ Add a Method
A method is a function inside a class. It can use the object's data with self.
class Car:
def __init__(self, brand):
self.brand = brand
def drive(self):
print(self.brand, "is driving")
my_car = Car("Toyota")
my_car.drive() # Toyota is driving
Call a method with the object name, a dot, and the method name.
๐ฅ Video: Create Car Class and Access a Method (Khmer)
๐ Make Many Objects
You can put objects in a list and use a loop to show them.
class Car:
def __init__(self, brand):
self.brand = brand
cars = [Car("Toyota"), Car("Honda"), Car("Ford")]
for car in cars:
print(car.brand)
Output:
Toyota
Honda
Ford
๐ฅ Video: Create a List of Car Objects and Print Them (Khmer)
โ Common Errors
Forget self
Methods need self as the first parameter:
class Person:
def say_hello(self):
print("Hello!")
Call the method with parentheses
class Person:
def say_hello(self):
print("Hello!")
person1 = Person()
person1.say_hello()
๐ก Tips
- A class is a plan. An object is made from the class.
- Use a capital letter at the start of a class name, like
Person. - Use
__init__to set up new objects. - Use
selfto access data and methods for the current object. - An attribute stores data. A method does an action.
- Use a list when you need to keep many objects together.
- Indent code inside the class and its methods.
โ Any Question?
Tell us in comment below! ๐
#Python #Class #Object #Method #LearnPython #Beginner