<p>我有一些代码,只有当一个特定的硬件设备连接运行。调试时我想放弃这个限制。你知道吗</p>
<p>为此,我尝试用一个只返回<code>True</code>的匿名函数临时替换检查硬件是否已连接的函数:</p>
<pre><code>import drm
def OnStartButton(self, evt):
# Don't check for dongle in debug mode.
if Globals.DEBUG:
# Store the original function so we can reassign it later.
func_store = drm.is_dongle_attached
drm.is_dongle_attached = lambda x: return True
if drm.is_dongle_attached():
do_some_stuff()
if Globals.DEBUG:
# Reassign function back to the original.
drm.is_dongle_attached = func_store
</code></pre>
<p>然而,这似乎不是一个合法的概念,因为我在网上看到一个<code>SyntaxError</code>:</p>
<pre><code>drm.is_dongle_attached = lambda x: return True
^
</code></pre>