<p>我最初的答案是错误的。。。(见历史)。你知道吗</p>
<p>它总是值得实现一个简化的版本来打破。。。见下文(使用这些<a href="https://github.com/Kenny2github/mw-api-client/blob/d1956632283c540b6275644a6b3d3782e5a794f4/mw_api_client.py#L1296-L1311" rel="nofollow noreferrer">1</a>、<a href="https://github.com/Kenny2github/mw-api-client/blob/d1956632283c540b6275644a6b3d3782e5a794f4/mw_api_client.py#L1323-L1325" rel="nofollow noreferrer">2</a>、<a href="https://github.com/Kenny2github/mw-api-client/blob/develop/mw_api_client.py#L120-L138" rel="nofollow noreferrer">3</a>、<a href="https://github.com/Kenny2github/mw-api-client/blob/develop/mw_api_client.py#L314-L318" rel="nofollow noreferrer">4</a>作为灵感)</p>
<pre class="lang-py prettyprint-override"><code>#!/usr/bin/env python3
from pprint import pprint
class Page(object):
def __init__(self, wiki, **data):
self.wiki = wiki
self.title = None
self.__dict__.update(data)
def __eq__(self, other):
return self.title == other.title
class Wiki(object):
def __init__(self, api_url):
self.api_url = api_url
def page(self, title, **evil):
if isinstance(title, Page):
return title;
return Page(self, title=title, **evil)
w = Wiki('url')
pprint(w)
pprint(w.__dict__)
p1 = w.page('testing')
pprint(p1)
pprint(p1.__dict__)
p2 = w.page('testing')
pprint(p2)
pprint(p2.__dict__)
p3 = w.page('testing something else')
pprint(p3)
pprint(p3.__dict__)
pprint(p1 == p2)
pprint(p1 == p3)
pprint(p1 in [ p2 ])
pprint(p1 in [ p2, p3 ])
</code></pre>
<p>输出:</p>
<pre class="lang-py prettyprint-override"><code><__main__.Wiki object at 0x7f2891957d30>
{'api_url': 'url'}
<__main__.Page object at 0x7f2891957dd8>
{'title': 'testing', 'wiki': <__main__.Wiki object at 0x7f2891957d30>}
<__main__.Page object at 0x7f2891957e48>
{'title': 'testing', 'wiki': <__main__.Wiki object at 0x7f2891957d30>}
<__main__.Page object at 0x7f289190cf60>
{'title': 'testing something else',
'wiki': <__main__.Wiki object at 0x7f2891957d30>}
True
False
True
True
</code></pre>
<p>如你所见,这是有效的。。。你知道吗</p>
<p>我不知道我对你使用<code>self.__dict__.update(data)</code>的感觉如何。。。第一次就让我措手不及。。。我现在怀疑它是否使用<a href="https://github.com/Kenny2github/mw-api-client/blob/d1956632283c540b6275644a6b3d3782e5a794f4/mw_api_client.py#L1311" rel="nofollow noreferrer">here</a>和<a href="https://github.com/Kenny2github/mw-api-client/blob/d1956632283c540b6275644a6b3d3782e5a794f4/mw_api_client.py#L1345" rel="nofollow noreferrer">here</a>(两行都做相同的事情……)</p>
<pre class="lang-py prettyprint-override"><code>class Page(object):
def __init__(self, wiki, getinfo=None, **data):
# ...
if getinfo is None:
getinfo = GETINFO
if getinfo:
self.__dict__.update(self.info())
def info(self):
# ...
self.__dict__.update(page_data)
return page_data
</code></pre>
<p>您能确保这些调用不会覆盖<code>title</code>吗?你知道吗</p>