self parameter refers to the current object of a class. It is used to access the attributes and methods that belong to that object. When a method is called using an object, Python automatically passes that object as the first argument to the method, which is conventionally named self.
class Mynumber:
def __init__(self, value):
self.value = value
def print_value(self):
print(self.value)
obj1 = Mynumber(17)
obj1.print_value()
Output
17
Explanation:
- obj1 = MyNumber(17) creates an object named obj1. Inside __init__(), self refers to obj1.
- self.value = value stores 17 in the object's value attribute. When obj1.print_value() is called, self again refers to obj1. Therefore, self.value accesses the value 17 and prints it.
Purpose of self
self parameter helps Python identify the object on which a method is called. It ensures that each object can access and maintain its own data independently. When a method is invoked through an object, Python automatically passes that object as self.
self in Constructors (__init__ Method)
__init__ method is automatically called when an object is created. The self parameter represents the newly created object and is used to initialize its attributes.
class Subject:
def __init__(self, sub1, sub2):
self.sub1 = sub1
self.sub2 = sub2
obj = Subject("Maths", "Science")
print(obj.sub1)
print(obj.sub2)
Output
Maths Science
Explanation:
- self.sub1 = sub1 creates an attribute named sub1 for the object.
- self.sub2 = sub2 creates an attribute named sub2 for the object.
- The values passed during object creation are stored in the object's attributes.
self in Instance Methods
Instance methods use self to access the attributes and other methods of the object that calls the method.
class Car:
def __init__(self, model, color):
self.model = model
self.color = color
def show(self):
print("Model:", self.model)
print("Color:", self.color)
audi = Car("Audi A4", "Blue")
ferrari = Car("Ferrari 488", "Green")
audi.show()
ferrari.show()
Output
Model: Audi A4 Color: Blue Model: Ferrari 488 Color: Green
Explanation:
- self.model and self.color access the attributes of the current object. When audi.show() is called, self refers to audi.
- When ferrari.show() is called, self refers to ferrari. This allows each object to work with its own data.
self is Not a Keyword
Yes, self is not a Python keyword. It is simply a naming convention that Python developers follow to represent the current object. Although you can use any valid parameter name, using self makes the code easier to read and understand.
class Car:
def __init__(this, model, color):
this.model = model
this.color = color
def show(this):
print("Model:", this.model)
print("Color:", this.color)
audi = Car("Audi A4", "Blue")
audi.show()
Output
Model: Audi A4 Color: Blue
Explanation:
- Here, this is used instead of self. The program works correctly because Python does not require the parameter name to be self.
- However, using self is recommended because it is the standard convention.
self Refers to the Current Object
self parameter refers to the object that calls a method. It allows Python to identify and work with the attributes and methods of that specific object.
class Check:
def __init__(self):
print("Address of self:", id(self))
obj = Check()
print("Address of object:", id(obj))
Output
Address of self: 140737343039744 Address of object: 140737343039744
Explanation:
- id(self) returns the memory address of the current object.
- id(obj) returns the memory address of the object created.
- Both addresses are the same, showing that self refers to the object that invoked the method.
Using self to Modify Object Attributes
self parameter allows methods to access and update an object's attributes. This enables an object to maintain and modify its own state throughout its lifetime.
class Counter:
def __init__(self):
self.count = 0
def increment(self):
self.count += 1
def decrement(self):
self.count -= 1
def get_count(self):
return self.count
counter = Counter()
counter.increment()
counter.increment()
counter.decrement()
print(counter.get_count())
Output
1
Explanation:
- self.count stores the current count value and increment() increases the value by 1.
- decrement() decreases the value by 1 and get_count() returns the current value of count.
- After two increments and one decrement, the final value becomes 1.
Note: To modify an object's state, update its attributes using self.attribute. Assigning a new value directly to self does not change the original object.