3 Respostas
+ 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')
+ 2
Thanks. Grateful!
+ 2
if you didn't need to declare any initial attributes to create a class instance, then leaving the __init__ dunder would be fine.