Arbitrary Argument Lists in Python

What is the output of this code snippet?

 

[python]
def f(a, *arguments):
print(a)
for arg in arguments:
print(arg)

f("A", "B", "C")
[/python]

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

Solution

A
B
C

 

Leave a Comment

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