<p>注意:Python的flyweight实现的一部分</p>
<pre class="lang-py prettyprint-override"><code>import weakref
class CarModel:
_models = weakref.WeakValueDictionary()
def __new__(cls, model_name, *args, **kwargs):
model = cls._models.get(model_name)
if not model:
model = super().__new__(cls)
cls._models[model_name] = model
return model
def __init__(self, model_name, air=False):
if not hasattr(self, "initted"):
self.model_name = model_name
self.air = air
self.initted=True
</code></pre>
<p>问题1 <code>super()</code>是什么意思?这是否意味着<code>CarModel</code>的父类?</p>
<p>问题2我也很难理解函数<code>__new__</code>是如何工作的?具体来说,下面这一行。</p>
<pre><code>model = super().__new__(cls)
</code></pre>
<p><code>__new__</code>的说明:</p>
<blockquote>
<p>The constructor function is called <code>__new__</code> as opposed to <code>__init__</code>, and
accepts exactly one argument, the class that is being constructed (it
is called before the object is constructed, so there is no self
argument). It also has to return the newly created object.</p>
</blockquote>