In Python, the `__init__()` method is a special method used for initializing newly created objects. It is called automatically when a new instance of a class is created. The `__init__()` method allows you to initialize attributes of the object and perform any necessary setup operations.
Here's an example:
```python
class MyClass:
def __init__(self, name):
self.name = name
def display_name(self):
print("Name:", self.name)
Creating an instance of MyClass
obj = MyClass("John")
Calling the display_name method
obj.display_name()
```
In this example, when we create an instance of `MyClass` using `obj = MyClass("John")`, the `__init__()` method is automatically called with the argument `"John"`. Inside the `__init__()` method, `self.name = name` initializes the `name` attribute of the object with the value `"John"`.
The `self` variable is a reference to the current instance of the class. It is used to access variables and methods within the class. When you call a method on an object, Python automatically passes the object itself as the first argument to the method. By convention, this first parameter is named `self`, but you can use any name you like.
In the example above, `self.name` refers to the `name` attribute of the current object. When we call `obj.display_name()`, `self` refers to the `obj` object, and `self.name` accesses the `name` attribute of that object.