Fundamentals 4 min read

What Do self.name, self.x, and self.num Mean in Python Classes?

This article explains why Python code uses self.name = name, how attributes like self.x and self.y can be accessed without explicit parameters, and what self.num = x does, providing clear examples and a concise summary for beginners.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
What Do self.name, self.x, and self.num Mean in Python Classes?

1. Introduction

Hello, I am a Python enthusiast. Recently a member asked why some code uses self.name = name, why some __init__ methods can use self.x and self.y without explicit parameters, and what self.num = x means.

Question examples (images):

2. Explanation

The assignment self.name = name stores the value passed as the parameter name into the instance attribute name, allowing the object to retain that value for later use.

When an attribute is defined directly in the class without being passed as a parameter, it is still accessed via self because it belongs to the instance; self.x and self.y refer to class‑level variables.

Similarly, self.num = x assigns the external variable x to the instance attribute num, making it available throughout the class.

3. Conclusion

This article clarified a common Python basics question, providing clear explanations and code examples to help readers understand instance attributes and the use of self in class definitions.

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.

PythonOOPbasicsselfinstance attributes
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

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.