inheritance - How to perform common post-initialization tasks in inherited Python classes? -


The initial process of grouping a common parent group can be divided into three parts: the normal part 1, Class-specific parts, normal part 2 At present, the first two parts are called by the __int_function of each child's class, but the second common part can be said separately, for example:

  Class BaseClass: def __init __ (self): Print 'base __init__' self.common1 () def com Mon1 (self): print 'mango1' DRA finalized initialization (self): print 'final initial [normal 2]' class sub-class 1 (base class): def __init __ (self): base class. __ init __ (self) Self.specific () DEF specific (self): print 'specific' if __name__ == '__main__': s = subclass1 () # Do not forget to finalize the initialization. Final Ideation () # Now the object has been fully started  

Is there no need to call final? Initialization ()?

Edit In order to finalize a call call, __init__ (in the form of) in subclass 1 makes life easier, but still the first one Remember to be completed in the form, at this time inside the "constructor". There is no way to implement full initialization in any way, which is what I see.

version 1 - delegate everything.

class subclass 1 (base class): def __init __ (self): super (subclass 1, own) .__ init__ () self.specific () super (subclass1, self). FinalizeInitialization ()

version 2 - delegate only one step

  class BaseClass: def __init __ (self): print 'base __init__' self.common1 ( ) Self (specific): Specific options (normal): # Two options: #If this is "abstract", then it is "abstract". : Extend an exception if it is "concrete": pass  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -