Fundamentals 5 min read

Understanding the self Parameter in Python Classes

This article explains the purpose of the self parameter in Python classes, using analogies and concrete code examples to show how self represents the instance itself, how it is passed automatically, and how different objects produce distinct self values when methods are invoked.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Understanding the self Parameter in Python Classes

When learning Python object‑oriented programming, many beginners are puzzled by the self parameter that appears in every instance method.

The article concludes that after a class is instantiated, self simply refers to the instance (object) itself, much like an identity card that uniquely identifies a person.

To illustrate, the author likens self to an ID card: the instance uses this card to call class methods, just as a person uses an ID to access services.

Since a Python class cannot be used directly, an object must be created first. Each object is unique and can invoke the class’s methods and attributes. The class acts as a “soul” that gives each instance its own self functionality.

Example code:

<code>class Students:
    # constructor
    def __init__(self, name):
        self.name = name

    # instance method
    def study(self, examination_results):
        self.examination_results = examination_results
        print("同学{}的考试分数是{}".format(self.name, self.examination_results))
        print("该实例对象的地址是{}".format(self))
</code>

Two instances are created:

<code>studend_a = Students('studend_a')
print(studend_a.name)

studend_b = Students('studend_b')
print(studend_b.name)
</code>

Calling the study method on each instance produces output that includes the student’s name, the exam score, and the memory address of that particular object, demonstrating that self points to the specific instance:

同学studend_a的考试分数是80 该实例对象的地址是<main.Students object at 0x...>
同学studend_b的考试分数是80 该实例对象的地址是<main.Students object at 0x...>

Thus, self.name is equivalent to instance.name , and printing self shows the object’s address, confirming that self always represents the current instance.

In summary, after a class is instantiated, self is the reference to that specific object, enabling each instance to maintain its own state and behavior.

PythontutorialoopclassselfInstance
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

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.