擅长:python、mysql、java
<p>这是一个很好的方法,我并不特别认为这是一个欺骗。这里是对<code>__init__</code>函数的一些增强,这样当您添加更多的类时,它就不必更改了。</p>
<pre><code>def __init__(self, *args, **kwargs):
super(Analysis, self).__init__(*args, **kwargs)
if not self.__type and type(self) == Analysis:
raise Exception("We should never create a base Analysis object. Please create a child proxy class instead.")
for _class in Analysis.__subclasses__():
if self.check_type == _class.__name__:
self.__class__ = _class
break
def save(self, **kwargs):
self.check_type = self.__class__.__name__
super(Analysis, self).save(**kwargs)
</code></pre>
<p>希望这有帮助!</p>