Python Forum
Do all class need constructors?(__init__)
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do all class need constructors?(__init__)
#11
Thank you @snippsat. Now I know what @staticmethod is doing. It's good for writing modules.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#12
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__
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#13
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.
Reply
#14
(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.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Initiating an attribute in a class __init__: question billykid999 8 1,249 May-02-2023, 09:09 PM
Last Post: billykid999
Question __init__ of Child Class zero_fX0 4 1,570 Mar-22-2023, 05:23 PM
Last Post: deanhystad
  Python class doesn't invoke setter during __init__, not sure if's not supposed to? mtldvl 2 3,246 Dec-30-2021, 04:01 PM
Last Post: mtldvl
  Not including a constructor __init__ in the class definition... bytecrunch 3 11,530 Sep-02-2021, 04:40 AM
Last Post: deanhystad
  " Run constructors" instead of the correct line MaartenRo 2 1,798 Aug-28-2020, 07:19 AM
Last Post: MaartenRo
  Why is there an __init__ statement within the __init__ definition iFunKtion 7 5,962 Feb-06-2017, 07:31 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020