Inheritance and Overriding __init__ in python

前端 未结 5 1651
春和景丽
春和景丽 2020-12-04 06:44

I was reading \'Dive Into Python\' and in the chapter on classes it gives this example:

class FileInfo(UserDict):
    \"store file metadata\"
    def __init_         


        
5条回答
  •  生来不讨喜
    2020-12-04 07:24

    You don't really have to call the __init__ methods of the base class(es), but you usually want to do it because the base classes will do some important initializations there that are needed for rest of the classes methods to work.

    For other methods it depends on your intentions. If you just want to add something to the base classes behavior you will want to call the base classes method additionally to your own code. If you want to fundamentally change the behavior, you might not call the base class' method and implement all the functionality directly in the derived class.

提交回复
热议问题