Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duck typing to allow tuples, lists, or something that can be treated like one

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

You'll note that foo() can function perfectly regardless of whether it's passed spam as a list, as a tuple, or really as anything that enforces an order to the element and can be addressed like a list.

How do you document this fact without telling the user to use a specific type?

like image 787
Ben Mordecai Avatar asked Feb 18 '23 14:02

Ben Mordecai


1 Answers

You say,

"""spam is an object that supports indexing."""
like image 122
Jakob Bowyer Avatar answered Feb 20 '23 12:02

Jakob Bowyer