[Tutor] variable number of inputs

wesley chun wescpy at gmail.com
Thu Jan 8 04:22:50 CET 2009


On Wed, Jan 7, 2009 at 7:01 PM, bob gailer <bgailer at gmail.com> wrote:
> Mr Gerard Kelly wrote:
>>
>> How can you make a function accept a variable number of inputs without
>> any particular limit?
>>        :
>> But what if you want to be able to call the function and put in as many
>> arguments as you want.
>
> def func(*a) # a will be a tuple of whatever arguments are passed in the call.


if any of the variables are keyworded, you'll need a vararg dictionary
too, **kwargs.

a very popular idiom you'll see in function signatures looks like this:

def func(*args, **kwargs)

this is the most flexible Python function definition because this
function can accept *any* number and type of arguments you can give
it, i.e.,

func()
func(a)
func(a, b)
func(a, b, c, d, e)
func(a, b, c, d, e=0)
func(a, b, 999, xxx=['foo', 'bar'], yyy=42)
        :

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list