1

I've been reading about the difference between a python constructor (__new__) vs. an initializer (__init__) on SO.

Given that __new__ is used to create class instances, and __init__ is used to customise those instances.

Why do most examples & working code of python classes built only with __init__?

Examples here and here (search "5. Classes")

Community
  • 1
  • 1
snowbound
  • 1,692
  • 2
  • 21
  • 29
  • 1
    When you initialize an object with `__init__(self, *args, **kwargs)` by issueing `newObj = MyClass(*args, **kwargs)`, Python already did the job of creating it for you with `__new__` and `self` is a reference to the new object. More info in the duplicate. – timgeb Jan 16 '16 at 12:29
  • 1
    Note that your classes inherit the default `__new__` from `object`, and most of the time that's adequate, unless you're doing stuff like deriving from one of the built-in types. – PM 2Ring Jan 16 '16 at 12:39
  • @timgeb. Thought it was other way round. From py3's `__new__` doc: `If __new__() returns an instance of cls, then the new instance’s __init__() method will be invoked like __init__(self[, ...]), where self is the new instance and the remaining arguments are the same as were passed to __new__(). If __new__() does not return an instance of cls, then the new instance’s __init__() method will not be invoked. ` – snowbound Jan 21 '16 at 04:49
  • I don't see anything in that quote that implies it would be the other way round. – timgeb Jan 21 '16 at 11:04

0 Answers0