Self Variable in _init_ constructor
* 'Self' is a default variable that contains the memory address
of the instance of the current class which is under current usage .
* So , one can use 'self' instance variable to refer to all the
instance variables and Instance Methods .
* When an instance of the class is created , the
instance name contains the memory location of the instance . The
memory location is internally passed to the 'self' . For example , one can
create an instance of the Student Class in the given manner :
S1 = Student()
* Here , 's1' contains the memory address of the instance . This
memory address is internally and by default passed to the 'self' variable .
* Since the 'self' knows the memory address of the instances ,
it can refer to all the members of the instance .
* One can use 'self' in the following ways :
1) The 'self' variable is used as a first parameter within the
constructor as :
def
_init_(self):
In this case , 'self' can be used to refer to the instance
variables inside the constructor .
2) 'self' can be used as a first parameter in the instance
method as :
def talk(self):
* Here , talk() is an instance method as it acts on the instance
variables present within the Class and defined under the _init_(self): method .
* If the method wants to act on the instance variables , then the method should know the memory location of the instance variables . That memory location is by default available to the talk() method through the 'self' method .
No comments:
Post a Comment