Why is my __init__ method not being called when I create a class instance?

Gihin Oha
Member
Kayıt: 2025-02-16 08:26:47
2025-02-16 08:57:25

 

I’m learning about object-oriented programming in Python, and I’ve written this simple class with an __init__ method to initialize the object:

 
class MyClass: def __init__(self, name): self.name = name obj = MyClass("Test")

However, when I create an instance of MyClass, it seems like the __init__ method isn't being called, and I don’t understand why. Could someone explain why this might happen? Are there any common pitfalls with the __init__ method that I should watch out for?