Python Forum
Do all class need constructors?(__init__) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Do all class need constructors?(__init__) (/thread-2723.html)

Pages: 1 2


RE: Do all class need constructors?(__init__) - wavic - Apr-05-2017

Thank you @snippsat. Now I know what @staticmethod is doing. It's good for writing modules.


RE: Do all class need constructors?(__init__) - volcano63 - May-02-2017

I know, it's an old thread - I've stumbled on it by chance - but there's one thing no responder has explicitly stated -
Quote:explicit better than implicit
Smile , right?
__init__ method is not a constructor; Python constructor is __new__


RE: Do all class need constructors?(__init__) - Mekire - May-02-2017

This thread was indeed a little old to necro for not much reason but since we are here...

From a technical perspective you are correct; __new__ is the constructor. But from a practical standpoint, for all intents and purposes this distinction is meaningless. __init__ is the constructor as you are generally taught to think about them.


RE: Do all class need constructors?(__init__) - volcano63 - May-02-2017

(May-02-2017, 09:21 AM)Mekire Wrote: But from a practical standpoint, for all intents and purposes this distinction is meaningless.  __init__ is the constructor as you are generally taught to think about them.
I don't think that it's meaningless. And I think that distinction is important.

Understanding this distinction is essential when explaining why __init__ should not contain return statement. And how __dict__ attribute "magically" appears in descendants of object class (Examples from recent discussions)

I don't remember C++ constructors too well - 9 years of abstention and going Dance , but I remember they had this ugly initialization parts. This is what __init__ does - in much clearer way.