self in Python class
self represents the instance of the class. By using the “self” we can access the attributes and 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 parameter of methods is the instance the method is called on.
In more clear way you can say that SELF has following Characteristic-
Self is always pointing to Current Object.
Python3
#it is clearly seen that self and obj is referring to the same object class check: def __init__( self ): print ( "Address of self = " , id ( self )) obj = check() print ( "Address of class object = " , id (obj)) # this code is Contributed by Samyak Jain |
Address of self = 140124194801032 Address of class object = 140124194801032
Another Example of Using SELF:
Python3
# Write Python3 code here 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 their attributes audi = car( "audi a4" , "blue" ) ferrari = car( "ferrari 488" , "green" ) audi.show() # same output as car.show(audi) ferrari.show() # same output as car.show(ferrari) #note:we can also do like this print ( "Model for audi is " ,audi.model) print ( "Colour for ferrari is " ,ferrari.color) #this happens because after assigning in the constructor the attributes are linked to that particular object #here attributes(model,colour) are linked to objects(audi,ferrari) as we initialize them # Behind the scene, in every instance method # call, python sends the instances also with # that method call like car.show(audi) |
Model is audi a4 color is blue Model is ferrari 488 color is green
Self is the first argument to be passed in Constructor and Instance Method.
Self must be provided as a First parameter to the Instance method and constructor. If you don’t provide it, it will cause an error.
Python3
# Self is always required as the first argument class check: def __init__(): print ( "This is Constructor" ) object = check() print ( "Worked fine" ) # Following Error is produced if Self is not passed as an argument Traceback (most recent call last): File "/home/c736b5fad311dd1eb3cd2e280260e7dd.py" , line 6 , in <module> object = check() TypeError: __init__() takes 0 positional arguments but 1 was given # this code is Contributed by Samyak Jain |
Self is a convention and not a Python keyword .
self is parameter in Instance Method and user can use another parameter name in place of it. But it is advisable to use self because it increases the readability of code, and it is also a good programming practice.
Python3
# Write Python3 code here class this_is_class: def __init__(in_place_of_self): print ( "we have used another " "parameter name in place of self" ) object = this_is_class() |
we have used another parameter name in place of self
Please Login to comment...