+ 1
What is self in python??
3 ответов
+ 2
Simply a reference of an object to itself
class Animal:
def __init__(name):
self.name = name
dog = Animal("dog")
cat = Animal("bird")
print(dog.name)
print(cat.name)
Output:
dog
bird
0
you forgot to write self inside ()
0
self is a convention made by programmers, could be used other names but to maintain a standard in coding you should keep writing it with self. When you call a class as bingo = Animal('animals name here') you pass two arguments to the class code u wrote. First argument is the variable itself, second argument is what u put inside (), that is 'animal name here'.