無論這是否是個好主意,我想知道是否可以強制某種方法接受某個輸入,例如不帶引號(「!」)的字符。示例:如何強制Python的函數接受某個輸入?
def special_print(text):
"""Print <text> unless ! (no quotes!) is passed as argument."""
if text == !:
print("Easter egg!")
else:
print(text)
special_print("Hello, World"!)
>>> Hello, World!
special_print("!")
>>> !
special_print(!)
>>> Easter egg!
會這樣嗎?只是好奇。
除非你願意到餐桌的Python用自己的語法。 – TigerhawkT3