TUTORIAL: Pass A Variable Num. of Args to a Python Function

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6251
  • Loc: Seattle, WA

Post 3+ Months Ago

Introduction


Note: Trying to do this in PHP? Check this out.

When declaring a function, the type and number of arguments are usually fixed at compile time. But sometimes it is necessary for a function to be able to accept a variable number of arguments unknown until run-time.

This tutorial will show you how to create a Python function that can accept a variable number of arguments.

In A Nutshell


If you're simply looking for an example to go off of, I won't keep you waiting. Here's an example of a function sum() that adds up all of the integers passed to it and returns the sum:

PYTHON Code: [ Select ]
#!/usr/bin/python
def sum(*args):
   total = 0
   for i in args:
      total += i
   return total
 
total = sum(1, 2, 3, 4, 5)
print "Total: ", total
  1. #!/usr/bin/python
  2. def sum(*args):
  3.    total = 0
  4.    for i in args:
  5.       total += i
  6.    return total
  7.  
  8. total = sum(1, 2, 3, 4, 5)
  9. print "Total: ", total


How It Works


Let's look at the code line by line:

PYTHON Code: [ Select ]
def sum(*args):

We declare our function. When this function is called, a tuple will be created from the arguments passed to the function, and this tuple will be stored in the variable args.

PYTHON Code: [ Select ]
total = 0

Here we simply create a local variable to hold our return value as we add the individual integers to it.

PYTHON Code: [ Select ]
for i in args:
   total += i
  1. for i in args:
  2.    total += i

Here we loop through the list of arguments and add their values to the total. Since tuples are iterable with a simple for-loop, no extra work is needed.

PYTHON Code: [ Select ]
return total

Finally, we return our total.

A Handy Tool: Create A List


One special thing to note is that because the arguments are stored in an immutable tuple, we can convert this tuple to a list using the list() constructor:

PYTHON Code: [ Select ]
arg_list = list(args)


In doing so, we are now able to use list methods such as pop() on our arguments, and we can eliminate the temporary iteration variable i:

PYTHON Code: [ Select ]
while arg_list:
   total += arg_list.pop()
  1. while arg_list:
  2.    total += arg_list.pop()


Conclusion


You should now know how to create a Python function that accepts a variable number of arguments at run-time.

I always welcome questions or feedback about this tutorial. Simply post a reply or PM me; I'm glad to help!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

Post Information

  • Total Posts in this topic: 1 post
  • Moderator: Tutorial Writers
  • Users browsing this forum: No registered users and 2 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.