[Tutor] order of arguments in function

Dick Moores rdm at rcblue.com
Wed Dec 6 13:29:54 CET 2006


At 02:28 AM 12/6/2006, Michael Lange wrote:
>On Tue, 05 Dec 2006 22:18:02 -0800
>Dick Moores <rdm at rcblue.com> wrote:
>
> > Say I have a function,
> >
> > def my_function(max, min=0):
> >      return max - min
> >
> > The order of arguments is counterintuitive, but it seems it can't be
> > changed if I want to have a default min. Is there way to write
> >
> > def my_function(min=0, max):
> >       stuff
> >
>
>def my_function(min, max=None):
>     if max is None:
>         max = min
>         min = 0
>     # stuff

Great!

>I am not sure if this is more intuitive though.

 >>>
def my_function(min, max=None):
     if max is None:
         max, min = min, 0
     return max - min
 >>> my_function(3, 7)
4

I meant that the order "min, max" is more intuitive than "max, min". 
Don't you agree? And it's the order used in random.randint(), 
random.randrange(), and random.uniform(), for examples.

Anyway, thanks, Michael.

Dick




More information about the Tutor mailing list