Arbitrary Argument Lists in Python

What is the output of this code snippet?


def f(a, *arguments):
for arg in arguments:

f("A", "B", "C")

This puzzle introduces a nice Python feature: arbitrary argument lists. Suppose, you want to create a function that allows an arbitrary number of arguments. An example is recognizing faces in images where each image consists of one or more pixel arrays. You can achieve this by adding an argument *pixelArrays to your function. This argument is a tuple which you can access via indexing or iteration in a for loop.

You can combine both types of arguments: formal arguments (e.g. a in the puzzle) and an arbitrary argument list (e.g. *arguments in the puzzle). If called with many arguments, the arbitrary argument list will handle all but the formal arguments.


Are you a master coder?
Test your skills now!


Related Video




Leave a Comment

Your email address will not be published. Required fields are marked *