Why not __new__ gets called before __init__ ? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Why not __new__ gets called before __init__ ?

As I read in object lifecycle topic, that __new__ method occurs just before instantiation. But when I run below code, I always get printed "init calling", not getting printed "new calling" before "init calling". Why? class ABC: def __init__(self): print("init calling") def __new__(self): print("new calling") def __call__(self): print("call calling") a = ABC()

12th Jul 2018, 1:54 PM
Akshay Babar
Akshay Babar - avatar
3 Respostas
+ 1
where u read that?
12th Jul 2018, 2:05 PM
$Ā¢šŽā‚¹š”­!šØš“
$Ā¢šŽā‚¹š”­!šØš“ - avatar
12th Jul 2018, 2:22 PM
Akshay Babar
Akshay Babar - avatar
0
If you want to control creating your object, you need write your own code, which creates the object (using super) and then return it. Your __new__ does not create object. https://stackoverflow.com/questions/674304/why-is-init-always-called-after-new
12th Jul 2018, 2:37 PM
ŠœŠøŠŗŠ¾Š»Š° Š¤ŠµŠ“Š¾ŃŃ”Ń”Š²
ŠœŠøŠŗŠ¾Š»Š° Š¤ŠµŠ“Š¾ŃŃ”Ń”Š² - avatar