0 votes
in Python by
What is self variable in Python?

1 Answer

0 votes
by

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.

Related questions

0 votes
asked Jan 18, 2021 in Python by SakshiSharma
+2 votes
asked Jan 1, 2022 in Python by sharadyadav1986
...