"SELF" in Python
The self in python represents the instance of CLASS .unlike this in c++, self is not a keyword , it is only a coding convention , often the first argument of the method is called self , you could give any name to the first argument you want
But a python user is strongly advised to stick to the convention provided . it binds the attributes with the given arguments . the use of self makes it easier to distinguish between instance attributes from local variables
Example:
class name:
def __init__ (s2,roll,sink): # as you can see i have used s2 instead of self
s2.roll = roll # declaring instance variable
s2.sink = sink
def single(self): # and here i have used self
print(self.roll, self.sink)
n1 = name(34,56)
n1.single()
you could declare variable within a class without using the self reference , but then those variables will be shared by all instances of that class . if you don't declare an instance variable then you cant pass unique value to each methods .
Thank you!!!
do share ,subscribe and comment
And DO like my facebook page on python
Like_here_Facebook
Comments
Post a Comment