INIT FUNCTION IN PYTHON| QA SDET | TEST AUTOMATION

Опубликовано: 26 Апрель 2026
на канале: Viplove QA - SDET
787
1

🔹 What is __init__?

_init_ is the constructor method in Python classes.

It runs automatically when a new object is created.

Used to initialize instance variables.



---

✅ Basic Syntax:

class Person:
def __init__(self, name, age):
self.name = name # instance variable
self.age = age

Usage:

p = Person("Alice", 25)
print(p.name) # Alice


---

🟩 Key Points:

It is similar to constructors in other languages like Java/C++.

self refers to the current object (mandatory first parameter).

You can overload _init_ using default arguments:


class Book:
def __init__(self, title="Unknown"):
self.title = title


---

🟦 When is _init_ called?

Every time you create an object:

obj = Book("Python") # _init_ is automatically called