The following code works in Python 2.7 with _x.__dict__['c']=8
class _x:
def __init__(self):
self.a = 6
self.b = 7
_x.__dict__['c']=8
print("good!")
y=_x()
print(y.__dict__)
print(_x.__dict__)
Output:
good!
{'a': 6, 'b': 7}
{'c': 8, '__module__': '__main__', '__doc__': None, '__init__': <function __init__ at 0x00000000049227B8>}
The above code does not work in Python 3.6 with
_x.__dict__['c']=8
and got errors:
TypeError Traceback (most recent call last)
<ipython-input-5-b4146e87f5a4> in <module>()
6 print("good!")
7
----> 8 y=_x()
9 print(y.__dict__)
10 print(_x.__dict__)
<ipython-input-5-b4146e87f5a4> in __init__(self)
3 self.a = 6
4 self.b = 7
----> 5 _x.__dict__['c']=8
6 print("good!")
7
TypeError: 'mappingproxy' object does not support item assignment
Any suggestions?