class extending dict type : Class Inheritance « Class « Python






class extending dict type

 

class defaultdict(dict):
    def __init__(self, default=None):
        dict.__init__(self)
        self.default = default

    def __getitem__(self, key):
        try:
            return dict.__getitem__(self, key)
        except KeyError:
            return self.default

   
  








Related examples in the same category

1.Use __class__, __bases__ and __dict__ for sub and super classUse __class__, __bases__ and __dict__ for sub and super class
2.Print out class treePrint out class tree
3.Class inheritedClass inherited
4.Inherited methodInherited method
5.Polymorphism: override the function from base class
6.Definition and test function for class Circle which is based on Point class
7.Derived class inheriting from a base class.
8.Multiple Inheritance
9.inheriting from multiple superclasses
10.Use Inheritance to add more features to a class
11.Class inheritance: inherit member variable override functionClass inheritance: inherit member variable override function