This or Self Pointer

What is this pointer?

In C++, this is a special pointer that points to the current object in a class. Python does not have a this pointer, but it uses self to refer to the current object. You can return self to enable method chaining just like in C++.

Why use it?

You use it when you want a function to return the current object β€” usually to allow method chaining (calling multiple functions on the same object in one line).

Example PythonΒ 

class Student:
def __init__(self):
self.roll_no = None
self.name = ""
def set_name(self, name):
self.name = name
return self # returning current object for chaining
def set_roll_no(self, roll_no):
self.roll_no = roll_no
return self # returning current object for chaining
def show(self):
print(f"Name: {self.name}, Roll No: {self.roll_no}")
# Usage
s = Student()
s.set_name("Ali").set_roll_no(5).show()
Name: Ali, Roll No: 5

πŸ‘‰ Explanation:

  • setName() sets the name and returns the current object using return *this;

  • setRollNo() also returns the object.

  • This allows chaining: s.setName("Ali").setRollNo(5);

βœ… 10.2 Separation of Interface and Implementation

In Python, separation is usually done using multiple files (like modules).

What does it mean?

  • The interface is the part of the class you see (function declarations).

  • The implementation is the part you write in a separate file (function definitions).

  • It helps keep code clean and manageable.

C++

πŸ“˜ Example:
1. Header File (Student.h) – interface only
class Student {
public:
Student& setName(const char* aName);
Student& setRollNo(int aNo);
void show();
private:
int rollNo;
char name[50];
};

2. Implementation File (Student.cpp) – actual code

#include "Student.h"
#include <iostream>
#include <cstring>
using namespace std;
Student& Student::setName(const char* aName) {
strcpy(name, aName);
return *this;
}
Student& Student::setRollNo(int aNo) {
rollNo = aNo;
return *this;
}
void Student::show() {
cout << "Name: " << name << ", Roll No: " << rollNo << endl;
}

3. Main File (main.cpp) – usage

#include "Student.h"
int main() {
Student s;
s.setName("Sara").setRollNo(10).show();
return 0;
}

PythonΒ 

πŸ“ File: student.py (this is the class definition)

class Student:
def __init__(self):
self.roll_no = None
self.name = ""
def set_name(self, name):
self.name = name
return self
def set_roll_no(self, roll_no):
self.roll_no = roll_no
return self
def show(self):
print(f"Name: {self.name}, Roll No: {self.roll_no}")

πŸ“ File: main.py (this is where we use the class)

from student import Student
s = Student()
s.set_name("Sara").set_roll_no(10).show()

βœ… Output:
Name: Sara, Roll No: 10

πŸ’‘ Benefits of Separating Interface and Implementation

  1. Implementation hiding – Users see only the interface, not the internal logic.

  2. Easier maintenance – You can change the logic in .cpp without touching .h.

  3. Code reusability – Same interface, different implementations (old/new).

  4. Reduced recompilation – Only implementation file changes, not dependent code.

  5. Improved readability – Cleaner and more organized codebase.

References

Last modified: Wednesday, 30 April 2025, 11:37 AM