Variadic Functions in Miranda

Here’s a function that will take an arbitrary number of ints as arguments in Python.

def sum(*nums):
    print(nums, " ")
    total = 0

    for num in nums:
        total += num

    print(total)

Within the function, the type of nums is equivalent to tuple. We can call len(nums), iterate over it with for, etc.

sum(1, 2)
sum(1, 2, 3)

If you already have multiple args in a list, apply them to a variadic function using func(*slice) like this.

nums = [1, 2, 3, 4]
sum(*nums)

To run the program, save the code in a file and execute it with Python.

$ python variadic-functions.py
(1, 2)  
3
(1, 2, 3)  
6
(1, 2, 3, 4)  
10

Another key aspect of functions in Python is their ability to form closures, which we’ll look at next.