🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

self Parameter

AI & DATA SCIENCE // self-parameter

self is the conventional name for the first parameter of every instance method, referring to the specific instance the method was called on.

Syntax

class MyClass:
    def method(self, other_param):
        return self.some_attribute

Deep Dive Course

When you call a method through an instance, Python automatically passes that instance as the first argument to the method, bound to whatever name is listed first in the method's parameter list — self is purely a naming convention, not a keyword, but virtually every Python codebase follows it. Inside the method, self is how you access that specific instance's attributes and call its other methods, which is why assigning to a self attribute in one method makes that value visible to every other method called on the same instance.

1Understanding self Parameter

When you call a method through an instance, Python automatically passes that instance as the first argument to the method, bound to whatever name is listed first in the method's parameter list — self is purely a naming convention, not a keyword, but virtually every Python codebase follows it. Inside the method, self is how you access that specific instance's attributes and call its other methods, which is why assigning to a self attribute in one method makes that value visible to every other method called on the same instance.

💡

Forgetting self as the first parameter in a method definition, or forgetting to write self. before an attribute you meant to access, are two of the most common mistakes for people newer to Python's OOP model — both usually surface as a confusing TypeError or AttributeError.

editor.html
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
    def area(self):
        return self.width * self.height

r = Rectangle(4, 5)
print(r.area())
localhost:3000

2Practical Example

Here is a real-world application of self Parameter showing how it is used in production Python code.

editor.html
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
    def area(self):
        return self.width * self.height

r = Rectangle(4, 5)
print(Rectangle.area(r))
localhost:3000

3Best Practices

Follow these guidelines when working with self Parameter:

1. Always name the first parameter of an instance method self, even though Python doesn't enforce the name, since every reader expects it

2. Access instance attributes and other methods through self inside instance methods, rather than trying to reference bare variable names

3. Remember self is passed automatically by Python when calling a method through an instance — you don't pass it explicitly at the call site

⚠️

Tip: Forgetting self as the first parameter in a method definition, or forgetting to write self. before an attribute you meant to access, are two of the most common mistakes for people newer to Python's OOP model — both usually surface as a confusing TypeError or AttributeError.

editor.html
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
    def area(self):
        return self.width * self.height

r = Rectangle(4, 5)
print(r.area())
localhost:3000

Examples

Example 01Basic Usage
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
    def area(self):
        return self.width * self.height

r = Rectangle(4, 5)
print(r.area())
Example 02Advanced Example
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
    def area(self):
        return self.width * self.height

r = Rectangle(4, 5)
print(Rectangle.area(r))

Best Practices

  • Always name the first parameter of an instance method self, even though Python doesn't enforce the name, since every reader expects it
  • Access instance attributes and other methods through self inside instance methods, rather than trying to reference bare variable names
  • Remember self is passed automatically by Python when calling a method through an instance — you don't pass it explicitly at the call site

Interview Question

Why does calling a method directly through the class, passing the instance explicitly, still work and produce the same result as calling it through the instance normally?

Hint: Think about what instance.method() actually desugars into.

Calling a method through an instance is really just convenient syntax that Python translates into calling it through the class, automatically supplying the instance as the first argument — accessing a method through an instance implicitly passes that instance as self. Calling the class's method directly and passing the instance explicitly does the exact same thing manually, which is why both forms behave identically.

Exercises

MediumPractice using self Parameter in a real scenario.
View Solution
class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
    def area(self):
        return self.width * self.height

r = Rectangle(4, 5)
print(r.area())

Frequently Asked Questions

Why does calling a method directly through the class, passing the instance explicitly, still work and produce the same result as calling it through the instance normally?

Calling a method through an instance is really just convenient syntax that Python translates into calling it through the class, automatically supplying the instance as the first argument — accessing a method through an instance implicitly passes that instance as self. Calling the class's method directly and passing the instance explicitly does the exact same thing manually, which is why both forms behave identically.

Related Functions

class-keywordinitinheritance