def foo(spam, obj_of_interest):
"""Pass a _____ and an object of interest, and return [something that does something worthwhile] """
name = spam[0]
quest = spam[1]
fav_color = spam[2]
# ... interesting code
return obj_of_interest
你会注意到 foo()
可以完美地运行,无论它是作为列表、元组还是作为任何强制执行命令的东西传递给 spam
到元素,可以像列表一样寻址。
如何在不告诉用户使用特定类型的情况下记录这一事实?
最佳答案
你说,
"""spam is an object that supports indexing."""
关于python - 鸭子类型(duck typing)以允许元组、列表或可以像一个一样对待的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14730383/