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
Studentclass with
nameand
scoreattributes.
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
<code>class Student():
def __init__(self, name, score):
self.name = name
self.score = score
def out(self):
print("%s:%s" % (self.name, self.score))
</code>Creating objects:
<code>Student1 = Student('Anny', '100')
Student2 = Student('Mike', '90')
</code>Access attributes with
Student1.nameand 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:
<code>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))
</code>The original code raised
UnboundLocalErrorbecause it tried to modify
numberwithout qualification; the corrected version uses
Student.number.
Class Methods
Methods that belong to the class itself use
clsand are marked with
@classmethod. Example prints the total number of students.
<code>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)
</code>Private Attributes and Methods
Attributes and methods prefixed with double underscores are private and cannot be accessed directly from outside the class. Use the
@propertydecorator to expose them safely.
<code>@property
def scores(self):
print("该学生成绩为%s" % self.score)
</code>Inheritance
Subclass inherits attributes and methods from a parent class, reducing code duplication. Example with a base class
SchoolMemberand subclasses
Teacherand
Student.
<code>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))
</code>Shows how to call parent methods using the class name or
super().
Using super()
<code>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))
</code>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.