in Education by
I'm learning the Python programming language and I've come across something I don't fully understand. In a method like: def method(self, blah): def __init__(?): .... .... What does self do? What is it meant to be? Is it mandatory? What does the __init__ method do? Why is it necessary? (etc.) I think they might be OOP constructs, but I don't know very much. Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
__init__ method():- It is known as a constructor in the object-oriented concepts. This method is called when an object is created from the class and it allows the class to initialize the attributes of a class. self:- self is used to represent the instance of the class. With using the “self” keyword we can access the attributes and methods of the class in python. Now let us understand these using an example:- class Point: def __init__(self, x, y): self._x = x self._y = y Here the __init__ method gets called when memory for the object is allocated: x = Point(1,2) If you want to persist the value with the object, it is important to use the self parameter inside an object's method. You implement the __init__ method like this for instance : class Point: def __init__(self, x, y): self._x = x self._y = y To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
    I am a learner and just want to know the basic concept of super(). Here in the given code I want to know ... () ChildOne() ChildTwo() Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    when do we use self in Python? In ruby we don't need to include this whereas in Python we have to, so ... Can someone explain me this? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    Is there any reason for a class declaration to inherit from an object? I just found some code that does this ... class code follows... Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    There are two codes, what are the difference between them: Using type import types if type(q) is types. ... ): do_something_else() Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Can someone please tell me the difference between a function decorated with @staticmethod and one with @classmethod? Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    What will be the output of the following Python code? class tester: def __init__(self, id): self.id = str(id) id="224" >>>temp ... >>>print(temp.id) a) 12 b) 224 c) None d) Error...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following Python code? class tester: def __init__(self, id): self.id = str(id) id="224" >>>temp ... >>>print(temp.id) a) 12 b) 224 c) None d) Error...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    Could someone please demystify interfaces for me or point me to some good examples? I keep seeing interfaces ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    Could someone please demystify interfaces for me or point me to some good examples? I keep seeing interfaces ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    Could someone please demystify interfaces for me or point me to some good examples? I keep seeing interfaces ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I've recently seen quite a bit of the following pattern in Python: class Foo: pass class Bar: ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
...