1.

What is self variable in Python?

Answer»

In Python, a self variable is used for binding the instance WITHIN the class to the instance inside the METHOD. In this, to access the instance variables and METHODS, we have to explicitly DECLARE it as the first method argument.

Example

class Dog:
    def __init__(self, breed):
        self.breed = breed
    def bark(self):
        print(F'{self.breed} is continuously barking.')
d = Dog('German Shepherd')
d.bark()

Output
German Shepherd is continuously barking.



Discussion

No Comment Found