0

I saw this code while learning how to add key:value pairs in a hashtable/Dictionaries. I couldn't understand the need and use of calling super().__init() in the constructor. Can someone pls help me out.

class MyDictionary(dict):  

# __init__ function 
def __init__(self): 
      
    super().__init__() 
    self = dict() 

    # Function to add key:value 
def add(self, key, value): 
      
    # Adding Values to dictionary 
    self[key] = value  
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • 1
    Whatever `dict.__init__` does, you would not want to skip what it's doing in your child class, would you? (On another note `self = dict() ` does not make much sense, what's that line supposed to do?) – timgeb Oct 07 '20 at 09:07
  • ohh. Right that makes sense. So does that means if I'm not planning to create a child class, I don't need to call 'super.__init()__' at all. Right? – Manas Tuteja Oct 07 '20 at 09:09
  • How would you be able to call `super().__init__(...)` when doing something other than coding a child class? – timgeb Oct 07 '20 at 09:11
  • I'm creating a hashmap to store a linked listed node as key and a clone-node as value. To build an add function, i referred to this code and got stuck. :/ – Manas Tuteja Oct 07 '20 at 09:11
  • 1
    @ManasTuteja sure but is that hashmap supposed to be a child class of `dict` or not? – timgeb Oct 07 '20 at 09:14
  • 1
    @timgeb yes, it does. I got it now. Thanks for the A2A. – Manas Tuteja Oct 07 '20 at 09:33
  • 1
    @trincot it did, thank you! – Manas Tuteja Oct 07 '20 at 09:34

0 Answers0