What is self parameter in python | Explain self parameter in python
Self Parameter :-
The self parameter in python is used to all instance in a class by using self parameter one can easily access all instance defined within a class including its method of attribute. self parameter is reference the current instance of the class and it also used to access variable that belong the class .
self parameter define a member function but do not specify it while calling the function. Self is first parameter of any function in the class.
Program :-
Output
Self Parameter |
Program :-
class Person:
def __init__(self,name,id):
self.name=name
self.id=id
p= Person ("Bharat",123)
print("Person id:",p.id)
print("Person name: ",p.name)
def __init__(self,name,id):
self.name=name
self.id=id
p= Person ("Bharat",123)
print("Person id:",p.id)
print("Person name: ",p.name)
Output
Person id: 123
Person name: Bharat
Person name: Bharat
Post a Comment