lundi 14 novembre 2016

May __init__ be used as normal method for initialization, not as constructor?

Sometimes it looks reasonable to use init as initialization method for already existing object, i.e.:

class A():
    def __init__(self, x):
        self.x = x

    def set_state_from_file(self, file):
        x = parse_file(file)
        self.__init__(x)

As alternative to this implementation I see the following:

class A():
    def __init__(self, x):
        self.init(x)        

    def init(self, x):
        self.x = x

    def set_state_from_file(self, file):
        x = parse_file(file)
        self.init(x)

It seems to me as overcomplication of code. Is there any guideline on this situation?

Aucun commentaire:

Enregistrer un commentaire