What is self in python?? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 1

What is self in python??

22nd Oct 2019, 11:18 AM
Tanuja Kulkarni
Tanuja Kulkarni - avatar
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
22nd Oct 2019, 11:27 AM
Trigger
Trigger - avatar
0
you forgot to write self inside ()
23rd Oct 2019, 10:35 PM
joao vitor
joao vitor - avatar
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'.
23rd Oct 2019, 10:39 PM
joao vitor
joao vitor - avatar