dimanche 14 février 2021

Inherit a dictionary vs a function returning "self.__dict__"

I am working on a team project and everyone is building their own prototype for fun. We are building a blockchain for research purposes and comparing each other's implementation. One person suggested that the Block class should inherit python's dictionary class. Another person suggested that the Block class should just have a dictionary. And another person suggested that the Block class should have a function that returns self.__dict__.

I guess I have never seen many people inherit a dictionary and wonder if this would be a proper design decision? I am also rather new to return self.__dict___ and it seems that can do everything (like turn all the information into python dictionary form/json).

My question is more of what are the benefits of each implementation and its restrictions.

Code example below:

class Block():

    def __init__(self, preHash, data):
         self.previousHash       = preHash
         self.signature          = data

    def toJson(self):
        return self.__dict__

VS

class Block():

    def __init__(self, preHash, data):
         self.d                  = {}
         self.d['previousHash']  = preHash
         self.d['signature']     = data

    def toJson(self):
        return self.__dict__

VS

class Block(dictionary):

    def __init__(self, preHash, data):
         self.previousHash       = preHash
         self.signature          = data

Aucun commentaire:

Enregistrer un commentaire