Python Duck键入以允许元组、列表或类似于元组的内容

Python Duck键入以允许元组、列表或类似于元组的内容,python,duck-typing,Python,Duck Typing,您将注意到,foo()可以完美地运行,而不管它是作为列表、元组还是作为任何对元素强制执行顺序并可以像列表一样寻址的对象传递的 你如何在不告诉用户使用特定类型的情况下记录这一事实 def foo(spam, obj_of_interest): """Pass a _____ and an object of interest, and return [something that does something worthwhile] """ name = spam[0] quest

您将注意到,
foo()
可以完美地运行,而不管它是作为列表、元组还是作为任何对元素强制执行顺序并可以像列表一样寻址的对象传递的

你如何在不告诉用户使用特定类型的情况下记录这一事实

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

正是我要找的。谢谢。或者更具体地说,“垃圾邮件是序列类型”。字典支持索引,但可能不适合,因为它不是有序的。(忽略具有适当整数键集的词典。)原文要求“执行元素顺序的任何内容”。给定函数的实现,我们可以看到,通过包含连续整数范围的键子集来模拟序列的字典符合标准;但并非所有字典都会这样。而且,通过具体化,您可以在不影响用户的情况下,在以后自由地使用类似于
name、quest、fav_color=spam
的内容更改实现。整型键控字典无法处理该更改。
"""spam is an object that supports indexing."""