user77180
0
Q:

why to use self in python

self represents the instance of the class. By using the “self” keyword we can access the attributes and methods of the class in python. It binds the attributes with the given arguments. 
9
By using the “self” keyword we can access the attributes 
and methods of the class in python.
It binds the attributes with the given arguments.
self is parameter in function and user can use another parameter 
name in place of it.
But it is advisable to use self because,
it increase the readability of code.
2
class food():
 
# init method or constructor
def __init__(self, fruit, color):
self.fruit = fruit
self.color = color
 
def show(self):
print("fruit is", self.fruit)
print("color is", self.color )
 
apple = food("apple", "red")
grapes = food("grapes", "green")
 
apple.show()
grapes.show()
-2

New to Communities?

Join the community