+ 2
self parameter in python class
Hello, Could you please explain what is happening with __init__ method and “self” parameter in the following example: class Cat: legs=4 test = Cat print(test.legs) Are __init__ and self initialized automatically?
8 ответов
+ 4
__init__ method is automatically called when you create object. And thee self refers to object.
Your class has no init method and object creation.
The legs variable is class variable. You can refer it by class name. It won't need object or self reference.
test = Cat making alias name for class. Not a object creation.
So test.legs refers to class variable legs. It is not instance variable. Instance variable need object reference like self.
Hope it helps.......
+ 3
You need to refer it by class name.
self.legs = Cat.legs
also by self as self.legs = self.legs So
actually you don't need to set
self.legs = Cat.legs.you can get it without copying so comment #self.legs = legs
print( test.legs) works fine. print 4.
+ 2
Yes, the __init__ method and the self parameter are automatically initialized when a new instance of a class is created in Python. The __init__ method is a special method in Python classes that is called when an instance of the class is created. This method can be used to initialize any attributes or variables that need to be set when an object is created.
The self parameter is a reference to the instance of the class that is being created. This parameter is automatically passed to the __init__ method when a new object is created, and it allows the method to access and modify the attributes of the newly created object.
+ 2
For example, the following code defines a Person class with a name attribute that is initialized in the __init__ method using the self parameter:
class Person:
def __init__(self, name):
self.name = name
person = Person("John")
print(person.name) # Output: "John"
https://code.sololearn.com/cwfDCHTOv4R3/?ref=app
In this code, the Person class has an __init__ method that takes a name parameter and sets it as the value of the name attribute using the self parameter. When a new Person object is created and passed to the __init__ method, the self parameter is used to set the name attribute of the newly created object.
+ 1
Jayakrishna🇮🇳, Thank you!
So as I understand here __init__ method hasn’t been created at all?
Also why the following code gives error saying that “name legs is not defined”? As I understand, If legs is class attribute it should work for the whole class:
class Cat:
legs=4
def __init__(self, color):
self.legs=legs
self.color=color
test = Cat("black")
print(test.legs)
+ 1
In the given example, __init__ is a method that is called when an instance of the Cat class is created. The self parameter is a reference to the instance of the class that is being created. This means that when a new Cat object is created, __init__ will be called and the newly created object will be passed as the self parameter.
In the example, legs is a class attribute that is defined outside of any methods in the Cat class. When the Cat class is defined, this attribute is added to the class and is shared by all instances of the class.
When test is created as an instance of the Cat class, the legs attribute is inherited from the Cat class and its value can be accessed through the test object. When test.legs is printed, the value of 4 is returned, which is the value of the legs attribute in the Cat class.
0
Calviղ Thank you a lot for so comprehensive answer!
0
Jayakrishna🇮🇳, Thank you. Now it is more clear.
However it is still strange why __init__ method doesn’t see the attribute legs and gives error when I write self.legs = legs