+ 2

Hello everyone! Can someone do without the __init__ method in OOP (python)? I have seen some codes without it.

Python OOP

30th Aug 2019, 2:51 PM
Powersurge
Powersurge - avatar
3 odpowiedzi
+ 7
When you don't need your objects to come with any initial data, you don't have to write init. You can write into the object dynamically after you created it. class C: pass c = C() c.x = 5 But you'll quickly see that init makes sense in most cases. If you create a hundred instances, you'd have to write in the data by hand for each and every one of them. Much more convenient if you can just write: c = C('Fritz', 52, 'single')
30th Aug 2019, 3:09 PM
HonFu
HonFu - avatar
+ 2
Thanks. Grateful!
30th Aug 2019, 3:13 PM
Powersurge
Powersurge - avatar
+ 2
if you didn't need to declare any initial attributes to create a class instance, then leaving the __init__ dunder would be fine.
31st Aug 2019, 5:16 AM
Shen Bapiro
Shen Bapiro - avatar