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 usingreturn *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
-
Implementation hiding β Users see only the interface, not the internal logic.
-
Easier maintenance β You can change the logic in
.cppwithout touching.h. -
Code reusability β Same interface, different implementations (old/new).
-
Reduced recompilation β Only implementation file changes, not dependent code.
-
Improved readability β Cleaner and more organized codebase.