Erik
0
Q:

what is self in python class

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 Class(parentClass):
    def __init__(self,arg):
        self.arg = arg
        
    def function(self):
        print(arg)
5
# 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) 
  
# Behind the scene, in every instance method  
# call, python sends the instances also with 
# that method call like car.show(audi) 
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