Master Python OOP: From Real‑World Thinking to Classes and Objects
This article introduces the fundamentals of object‑oriented programming in Python, contrasting procedural and OOP approaches, explaining classes, objects, their attributes and methods, and provides step‑by‑step code examples—including class definition, object creation, method invocation, and property manipulation—to help beginners grasp OOP concepts.
1. Introduction to Object‑Oriented Programming
In real life we think about a student introducing themselves, not the act of introduction first. This contrasts with code where the behavior is written before deciding who it applies to.
How to express our real‑world thinking in code?
Procedural programming: write code top‑down according to business logic.
Object‑oriented programming: bind data and functions together, encapsulate, enabling faster development and reducing duplicate code.
Today we learn a new programming paradigm: Object‑Oriented Programming (OOP).
Two very important OOP concepts: class and object.
An object is the core of OOP; to group objects with common features we define a class.
A class is like a blueprint for creating objects (e.g., a plane).
2. Analyzing Classes and Objects
1. Class
物以类聚,人以群分。
具有相似内部状态和运动规律的实体的集合(或统称为抽象)。
具有相同属性和行为事物的统称。Classes are abstract; we instantiate them to obtain concrete objects. One class can produce many objects.
2. Object
# 某一个具体事物的存在 ,在现实世界中可以是看得见摸得着的。
# 可以是直接使用的3. Relationship between Class and Object
Summary: a class is a template for creating objects.
4. Distinguishing Classes and Objects
# 奔驰smart 类
# 张三的那辆奔驰smart 对象
# 水果 类
# 苹果 类
# 红苹果 类 红富士苹果 类
# 我嘴里吃了一半的苹果 对象5. Components of a Class
A class consists of three parts:
Class name.
Attributes: a set of data.
Methods: operations that can be performed on objects.
Example:
1) Human design focuses on three things:
Class name: Person.
Attributes: height, age.
Methods: run, fight.
2) Dog class design:
Class name: Dog.
Attributes: breed, color, gender, name, number of legs.
Methods: bark, run, bite, eat, wag tail.
6. Class Abstraction
How to abstract everyday things into program classes? Objects sharing similar attributes and behaviors can be abstracted into a class. Usually nouns become class names.
1) Tank fires three shells, destroys two planes
Tank → class.
Shell → class.
Plane → class.
2) Xiao Ming on a bus holding a dog with a hot dog
Xiao Ming → Person class.
Bus → Vehicle class.
Hot dog → Food class.
Dog → Dog class.
3) Identify classes in the diagram
Explanation: Person, Gun, Bullet, Grenade, Knife, Box.
4) Identify classes in another diagram
Explanation: Sunflower (class xrk, attributes color, type, behavior expose sunlight), Pea (class wd, attributes color, hairstyle, health, behavior fire, shake head), Nut (class jg, attributes health, type, behavior block), Zombie (class js, attributes color, health, type, speed, behavior walk, run jump, eat, die).
3. Project
1. Define a Class
Define a class with the following format:
class ClassName:
# method listDemo: define a Cat class
# Define class
class Cat:
# method
def eat(self):
print("猫在吃鱼....")
def drink(self):
print("猫在喝可乐...")Note: there are new‑style and classic classes; Cat above is classic. Naming follows PascalCase.
2. Create an Object
In Python, create objects from a defined class: object_name = ClassName() Demo:
# Define a class
class Cat:
def eat(self):
print("猫在吃鱼....")
def drink(self):
print("猫在喝可乐...")
# Create an object
tom = Cat()3. Call Object Methods
class Cat:
def eat(self):
print("猫在吃鱼....")
def drink(self):
print("猫在喝可乐...")
tom = Cat()
tom.eat()
tom.drink()Result shown in image:
4. Add Attributes to an Object
class Cat:
def eat(self):
print("猫在吃鱼....")
def drink(self):
print("猫在喝可乐...")
tom = Cat()
# Add attributes
tom.name = "汤姆"
tom.age = 30
tom.eat()
tom.drink()
print(tom.name)
print(tom.age)Result shown in image:
5. Access Object Attributes via Methods
class Cat:
def eat(self):
print("猫在吃鱼....")
def drink(self):
print("猫在喝可乐...")
def introduce(self):
print("名字是:%s, 年龄是:%d" % (tom.name, tom.age))
tom = Cat()
# Add attributes
tom.name = "汤姆"
tom.age = 30
tom.eat()
tom.drink()
print(tom.name)
print(tom.age)
print("-"*30)
# Call method that accesses attributes
tom.introduce()Result shown in image:
4. Summary
This article uses everyday phenomena to introduce Python OOP basics, offering rich examples to help readers understand objects, and concludes with a small cat project to reinforce class and object concepts.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
