Since calling className() will execute the code in __init__(args), why in the code below, is someone explicitly calling __init__?
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
Is there any difference in the actual code that is executed between the two method calls, or is chossing __init__() over className() simply arbitrary?
Running Python 3.4
className() does more than call __init__. It also calls __new__, which creates a new instance of the class. So calling Frame() would not do the superclass initialization on the same self object; it would create a new object and initialize that.
You call __init__ when you want to run just __init__ on a particular instance you've already created. Typically this is in a situation like the one you show, where you want to let a superclass do its initialization on top of a subclass's initialization.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With