[Tutor] Check Number of Parameters Passed In
bob gailer
bgailer at gmail.com
Mon Jul 14 03:05:37 CEST 2008
James wrote:
> Hi All,
>
> I'm writing a function that has optional paramters (i.e., "foo(
> parameter , optionalParameter = None , optionalParameter2 = None )").
>
> Is there some way inside of foo() that I can check to see how many
> parameters have been passed in? I want to check to ensure that only
> *one* optional parameter is passed in (and if more than one is passed
> in, do some sort of error handling).
>
> Thanks!
> - j
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
As an alternative to Kent's suggestion consider:
def foo(parameter, *args)
args will be a tuple with 0, 1 or more items in it.
So you can test for len(args) == 1
--
Bob Gailer
919-636-4239 Chapel Hill, NC
More information about the Tutor
mailing list