Master Python OOP: Classes, Objects, and Inheritance Explained
This article introduces Python object‑oriented programming, covering classes, objects, attributes, methods, class vs. instance variables, class methods, private members, inheritance, and the use of super() with clear code examples.
Classes and Objects
A class describes a collection of objects that share the same attributes and methods; an object is an instance of a class. Example: a Student class with name and score attributes.
Attributes and Methods
Attribute : shared property of all objects, e.g., name and score.
Method : function defined inside a class, e.g., a method to print a student's information.
Defining a Class in Python
class Student():
def __init__(self, name, score):
self.name = name
self.score = score
def out(self):
print("%s:%s" % (self.name, self.score))Creating objects:
Student1 = Student('Anny', '100')
Student2 = Student('Mike', '90')Access attributes with Student1.name and call methods with Student1.out().
Class Variables vs Instance Variables
A class variable is shared across all instances; an instance variable belongs to each object. Example with a counter:
class Student():
number = 0
def __init__(self, name, score):
self.name = name
self.score = score
Student.number = Student.number + 1
def show(self):
print("%s:%s" % (self.name, self.score))The original code raised UnboundLocalError because it tried to modify number without qualification; the corrected version uses Student.number.
Class Methods
Methods that belong to the class itself use cls and are marked with @classmethod. Example prints the total number of students.
class Student():
number = 0
def __init__(self, name, score):
self.name = name
self.score = score
Student.number = Student.number + 1
def show(self):
print("%s:%s" % (self.name, self.score))
@classmethod
def people(cls):
print("一共有%s名学生" % Student.number)Private Attributes and Methods
Attributes and methods prefixed with double underscores are private and cannot be accessed directly from outside the class. Use the @property decorator to expose them safely.
@property
def scores(self):
print("该学生成绩为%s" % self.score)Inheritance
Subclass inherits attributes and methods from a parent class, reducing code duplication. Example with a base class SchoolMember and subclasses Teacher and Student.
class SchoolMember:
def __init__(self, name, age):
self.name = name
self.age = age
def tell(self):
print('Name:"{}" Age:"{}"'.format(self.name, self.age), end=" ")
class Teacher(SchoolMember):
def __init__(self, name, age, salary):
SchoolMember.__init__(self, name, age)
self.salary = salary
def tell(self):
SchoolMember.tell(self)
print('Salary: {}'.format(self.salary))
class Student(SchoolMember):
def __init__(self, name, age, score):
SchoolMember.__init__(self, name, age)
self.score = score
def tell(self):
SchoolMember.tell(self)
print('score: {}'.format(self.score))Shows how to call parent methods using the class name or super().
Using super()
class Student(SchoolMember):
def __init__(self, name, age, score):
SchoolMember.__init__(self, name, age)
self.score = score
def tell(self):
super().tell()
print('score: {}'.format(self.score))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.
Ops Development Stories
Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.
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.
