this question has answer here:
i want override __setattr__
method on python classes using python 3.4.3. found this answer not seem work error.
the full code follows:
class testclass(object): def __init__(self): self.x = 4 def __setattr__(self, name, value): # special given 'name' #default behavior below super(testclass).__setattr__(name, value) test = testclass()
and error is
traceback (most recent call last): file "client_1.py", line 26, in <module> test = testclass() file "client_1.py", line 17, in __init__ self.x = none file "client_1.py", line 23, in __setattr__ super(testclass).__setattr__(name, value) attributeerror: 'super' object has no attribute 'x'
i assume attribute x
cannot set not yet defined. , line trying define atribute (in __init__
) cannot define attribute, calls overwritten method...
how define attribute x
without implicity calling overwritten __setattr__
method then?
super
doesn't need arguments in python3 (at least in case of overriding instance methods):
super().__setattr__(name, value)
is equivalent of python2's
super(myclass, self).__setattr__(name, value)
which still works though.