擅长:python、mysql、java
<p>只需更新<code>__init__</code>中实例的<code>__dict__</code>:</p>
<pre><code>class Base:
def __init__(self, params):
for key in params:
if hasattr(type(self), key):
self.__dict__[key] = params[key]
</code></pre>
<p>然后:</p>
^{pr2}$
<p>孙子们会表现得:</p>
<pre><code>class GrandChild(Child):
field3 = None
gc = GrandChild(dict(field1="one", field2="two", field3="three"))
print(gc.field1) # "one"
print(gc.field2) # "two"
print(gc.field3) # "three"
</code></pre>