+ 1
What is self parameter in python ?
3 ответов
+ 1
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.
Source: https://www.geeksforgeeks.org/self-in-python-class/
+ 1
An example of self parameter would look like this
Class Person
def __init__(self, n):
self.name = n;
+ 1
Eg.
class User:
def __init__(self, name, age):
#called when initializing the class
self.Name = name
self.Age = age
#here, self refers to the current class (object) which is User, so we're setting Name and Age attributes from parameters
def say_hi(self):
print(self.Name, "says: Hi!")
me = User("username", 99)
#here me is a User class with attributes: Name="username" and Age=99
me.say_hi()
#username says: Hi!