Fundamentals 9 min read

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.

Ops Development Stories
Ops Development Stories
Ops Development Stories
Master Python OOP: Classes, Objects, and Inheritance Explained

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

<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.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:

<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

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.

<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

@property

decorator 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

SchoolMember

and subclasses

Teacher

and

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>
PythonoopClassesInheritanceprivate-attributesclass-methods
Ops Development Stories
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.