Example 1: Inheriting a built-in data type (dictionary)
Class example_class(dict):
def __init__(self):
dict.__init__(self)
self["key1"] = "value1"
self["key2"] = "value2"
self.random_variable = 15
Example 2: No dictionary inheritance (but gets the same "result")
Class example2_class:
def __init__(self):
self.example_dictionary = dict() # or alternatively {}
self.example_dictionary["key1"] = "value1"
self.example_dictionary["key2"] = "value2"
self.random_variable = 29
Can someone explain the difference between Example 1 & 2, like advantages/disadvantages?
By inheriting from the build-in type dictionary you can overwrite (some) build-in methods, however I've seen code which inherits from these build-in data types without redefining any existing method, in this case I don't see why you would try to inherit a build-in data type instead of just creating an object in your init() method.