- "self " represents the instance of the class.
- By uing the "self" keyword we can access the attributes nd methods of the class in python.it binds the attributes with the given arguments.
- The reason you need to use self is because python does not use the @syntax to refer to instance attributes.
- Python decided to do methods in a way that makes the instance to which the method belongs be passed automatically , but not received automatically .the first parameters of methods is the instance the method is called on.
Example
class car():
# init method or constructor
def_init_(self,model,color):
self.model=model
self.color=color
def show(self):
print("Model is" , self.model)
print("color is " , self.color)
#both objects have different self which contain theor attributes
audi=car("audi a4","blue")
ferrari=car("ferrai488" , "green")
audi.show()# same output as car.show(audi)
ferrari.show() #same output as car.show(ferrari)
OUTPUT
Model is audi a4
color is blue
Model is ferrari 488
color is green