Arbitrary Arguments (*args)
In Python, the *args syntax is used to pass a varying number of arguments to a function. The *args parameter allows you to pass any number of positional arguments to a function, and it collects them into a tuple.
Let's implement the multiply() function that can accept a variable number of arguments:
1234def multiply(*args): print(args, type(args)) multiply(12, 11, 52, 33)
The function multiply() accepts a variable number of arguments using the syntax *args. The arguments are then stored in a tuple called args. We can use a loop to multiply all the arguments together, as shown below:
1234567891011def multiply(*args): result = 1 for arg in args: print(result, "*", arg) result *= arg return result print("Result =", multiply(12, 11, 52, 33)) print("Result without args:", multiply())
Now, you can see that our function works with no arguments as well as with many arguments.
Note
The parameter
*argsis commonly referred to as variable-length argument or arbitrary argument list.
If we need a function that should take from one/two/... to many arguments, a combination of positional arguments and arbitrary arguments, such as *args.
12345678def multiply(first, second, *args): result = first * second for arg in args: result *= arg return result print("Correct:", multiply(5, 2, 5, 1, 3)) print("Not correct", multiply(1))
In the example above, you can see that our function returned 150 when called with 2 or more arguments and raised a TypeError when called with fewer than 2 arguments.
It is important to note that the positional arguments should be defined before the optional arguments:
123456789def multiply(first, second, *args): print("first =", first) print("second =", second) result = first * second for arg in args: result *= arg return result print("Result:", multiply(11, 22, first=33, second=44, 55))
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Mi faccia domande su questo argomento
Riassuma questo capitolo
Mostri esempi dal mondo reale
Awesome!
Completion rate improved to 4.35
Arbitrary Arguments (*args)
Scorri per mostrare il menu
In Python, the *args syntax is used to pass a varying number of arguments to a function. The *args parameter allows you to pass any number of positional arguments to a function, and it collects them into a tuple.
Let's implement the multiply() function that can accept a variable number of arguments:
1234def multiply(*args): print(args, type(args)) multiply(12, 11, 52, 33)
The function multiply() accepts a variable number of arguments using the syntax *args. The arguments are then stored in a tuple called args. We can use a loop to multiply all the arguments together, as shown below:
1234567891011def multiply(*args): result = 1 for arg in args: print(result, "*", arg) result *= arg return result print("Result =", multiply(12, 11, 52, 33)) print("Result without args:", multiply())
Now, you can see that our function works with no arguments as well as with many arguments.
Note
The parameter
*argsis commonly referred to as variable-length argument or arbitrary argument list.
If we need a function that should take from one/two/... to many arguments, a combination of positional arguments and arbitrary arguments, such as *args.
12345678def multiply(first, second, *args): result = first * second for arg in args: result *= arg return result print("Correct:", multiply(5, 2, 5, 1, 3)) print("Not correct", multiply(1))
In the example above, you can see that our function returned 150 when called with 2 or more arguments and raised a TypeError when called with fewer than 2 arguments.
It is important to note that the positional arguments should be defined before the optional arguments:
123456789def multiply(first, second, *args): print("first =", first) print("second =", second) result = first * second for arg in args: result *= arg return result print("Result:", multiply(11, 22, first=33, second=44, 55))
Grazie per i tuoi commenti!