Apr-05-2017, 06:24 PM
Thank you @snippsat. Now I know what @staticmethod is doing. It's good for writing modules.
Do all class need constructors?(__init__)
|
Apr-05-2017, 06:24 PM
Thank you @snippsat. Now I know what @staticmethod is doing. It's good for writing modules.
May-02-2017, 09:15 AM
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 ![]() __init__ method is not a constructor; Python constructor is __new__
Test everything in a Python shell (iPython, Azure Notebook, etc.)
May-02-2017, 09:21 AM
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.
May-02-2017, 09:36 AM
(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 ![]() __init__ does - in much clearer way.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
|
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
Initiating an attribute in a class __init__: question | billykid999 | 8 | 3,028 |
May-02-2023, 09:09 PM Last Post: billykid999 |
|
![]() |
__init__ of Child Class | zero_fX0 | 4 | 5,888 |
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 | 5,208 |
Dec-30-2021, 04:01 PM Last Post: mtldvl |
|
Not including a constructor __init__ in the class definition... | bytecrunch | 3 | 18,919 |
Sep-02-2021, 04:40 AM Last Post: deanhystad |
|
" Run constructors" instead of the correct line | MaartenRo | 2 | 2,606 |
Aug-28-2020, 07:19 AM Last Post: MaartenRo |
|
Why is there an __init__ statement within the __init__ definition | iFunKtion | 7 | 7,474 |
Feb-06-2017, 07:31 PM Last Post: Larz60+ |