Why is __init__ called during object creation instead of initialization?

0
17
Asked By CuriousCoder42 On

I've been wondering why the __init__ method is labeled as 'init' when it's really called after the object is instantiated. Shouldn't it be called __inst__ instead? It's a bit frustrating to me. Am I missing something, is there a technical reason behind it, or is it just traditional naming?

5 Answers

Answered By SyntaxSavant On

You're totally right about the distinction! __new__ is called first to allocate memory and create the object, while __init__ is where you initialize the object's settings. So, by the time you get to __init__, the object exists and that's why it takes 'self' as an argument.

Answered By TechWhiz87 On

The thing is, the __new__ method is what actually creates the object—this is known as instantiation. Once the object is created, __init__ is called to set everything up, hence why it’s considered initialization. So, they really serve two different purposes in the object lifecycle.

Answered By DevDude77 On

Just to clarify, __init__ has that name because it's about initializing the attributes of the new instance once it exists. Some languages might use different terms, but this is how it’s handled in Python. It's normal to have these naming conventions!

Answered By CodeNinjaX On

It's important to understand the difference between instantiation and initialization. Instantiation is when the object is created (via __new__), and initialization is when it's set up (via __init__). They aren’t the same, even if they happen closely together.

Answered By ByteBender On

If you're confused about the terms, think of it like this: creating an object is instantiation, and setting its initial values is initialization. That’s why we call it __init__! It's all part of the lifecycle of an object.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.