Fundamentals 9 min read

Master Python OOP: Classes, Inheritance, and Object Lifecycle Explained

This article introduces Python's object‑oriented programming fundamentals, covering class definitions, class and instance variables, the __init__ constructor, the role of self, inheritance, method overriding, built‑in class attributes, garbage collection, and private members, complemented by illustrative code snippets.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Python OOP: Classes, Inheritance, and Object Lifecycle Explained

Object‑Oriented Technology Introduction

Class: a blueprint describing a set of objects that share the same attributes and methods; an object is an instance of a class.

Class variable: shared across all instances, defined inside the class but outside any method; typically not used as an instance variable.

Data member: class or instance variables that store data related to the class or its objects.

Method overriding: when a subclass needs to modify an inherited method, it can override (replace) the method.

Instance variable: a variable defined within a method that belongs to the current instance.

Inheritance: a derived class inherits fields and methods from a base class, allowing a derived object to be treated as a base object.

Creating Classes

a is a class variable shared among all instances; it can be accessed via the class name P from inside or outside the class.

The special __init__() method is the constructor; it is called automatically when an instance is created. self represents the instance; it must be the first parameter of any class method, though callers do not pass it explicitly.

self represents the instance, not the class

Class methods differ from ordinary functions by requiring an extra first parameter, conventionally named self.

Input and output examples are shown in the following images.

From the results, self refers to the current object’s address, while self.__class__ points to the class. self is not a Python keyword; it can be renamed (e.g., to runoob) and the code still works.

Creating Instance Objects

Access attributes using dot notation; class variables can be accessed via the class name.

Python Built‑in Class Attributes

__dict__

: a dictionary containing the class’s attributes. __doc__: the class’s documentation string. __name__: the class name. __module__: the module where the class is defined. __bases__: a tuple of the class’s base classes.

Example usage is illustrated in the following image.

Python Object Destruction (Garbage Collection)

Python uses reference counting to track and reclaim unused objects.

Each object has a reference counter; when it drops to zero, the object is reclaimed, though not necessarily immediately.

The garbage collector also handles cyclic references that reference counting alone cannot resolve.

Class Inheritance

Inheritance enables code reuse by establishing type‑subtype relationships between classes.

Syntax: class DerivedClass(BaseClass): Base class constructors ( __init__()) are not called automatically; they must be invoked explicitly.

When calling a base class method, prepend the base class name and pass self as an argument.

Python searches for a method in the derived class first, then proceeds up the inheritance chain.

Multiple inheritance is possible by listing more than one base class.

Input and output examples are shown in images.

Method Overriding

Input and output examples are displayed in images.

Class Attributes and Methods

Private Attributes

__private_attrs

: double‑underscore prefix marks an attribute as private; it can only be accessed within the class via self.__private_attrs.

Class Methods

Define a method with def inside a class; the first parameter must be self.

Private Methods

__private_method

: double‑underscore prefix marks a method as private; it can only be called within the class via self.__private_method.

Underscore Conventions

__foo__

: special (magic) methods like __init__. _foo: protected attribute, intended for internal use and subclass access. __foo: private attribute, accessible only within the defining class.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonGarbage CollectionOOPclassesInheritanceprivate attributes
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

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.