擅长:python、mysql、java
<p>你在迂回地问另一个问题。Python是动态类型化的,因此函数将接受任何类型。您可以使用其他类型执行类似于所需的操作,也可以使用<code>Sentinel</code>对象。你知道吗</p>
<pre><code>Sentinel = object()
def special_print(text):
if text is Sentinel:
print("Easter egg!")
else:
print(text)
special_print(Sentinel)
</code></pre>
<p>不能使用<code>!</code>字符,因为这不是有效的变量名。也不必使用<code>Sentinel</code>对象,只需使用非字符串变量即可</p>
<pre><code>def special_print(text):
if isinstance(text, int):
print("Easter egg!")
else:
print(text)
special_print(1)
</code></pre>