python 3.x - Circular dependency in the class constructor -
python 3.x - Circular dependency in the class constructor -
i have next class:
class customdictionary(dict): def __init__(self, val, *args, **kwargs): self.wk = val super(dict, self).__init__() def __setattr__(self, key, value): if key in self.wk: raise exception("wrong key", "") key = key.replace(" ", "_") self.__dict__[key] = value def main(): wrong_keys = ("r23", "fwfew", "s43t") dictionary = customdictionary(wrong_keys) dictionary["a1"] = 1
as can see, create attribute wk
in constructor
. have __setattr__
function, in work attribute wk
. however, customdictionary object
has no attribute wk
.
__setattr__
pain way, because called every assignment instance member. easiest prepare situation define empty wk
before __init__
:
class customdictionary(dict): wk = [] def __init__(self, val, *args, **kwargs): self.wk = val ...
python-3.x constructor circular-dependency
Comments
Post a Comment