List Info

Thread: question about *args and functions




question about *args and functions
user name
2007-01-26 09:30:03
_______________________________________________ Tutor maillist - Tutorpython.org http://mail.python.org/mailman/listinfo/tutor
Re: question about *args and functions
user name
2007-01-26 09:41:00
_______________________________________________ Tutor maillist - Tutorpython.org http://mail.python.org/mailman/listinfo/tutor
Re: question about *args and functions
user name
2007-01-26 09:44:37
Greetings, You could default it to None and check in your script to see if it has changed. def some_function(req_var, req_var2, un_req_var=None): if un_req_var != None: dosomething else: dosomethingelse Wesley Brooks. On 26/01/07, shawn bright gmail.com> wrote: > lo there all, > > if i have a function that sometimes needs a value passed to it and sometimes > not, is this where i use *args ? > > like this > > def some_function(req_var, req_var2, un_req_var): > do some stuff > return value > > how would i use this if sometimes i need to process un_req_var and sometimes > not ? > > thanks > > _______________________________________________ > Tutor maillist - Tutorpython.org > http://mail.python.org/mailman/listinfo/tutor > > > _______________________________________________ Tutor maillist - Tutorpython.org http://mail.python.org/mailman/listinfo/tutor
Re: question about *args and functions
user name
2007-01-26 09:40:46
shawn bright wrote: > lo there all, > > if i have a function that sometimes needs a value passed to it and > sometimes not, is this where i use *args ? No, use an optional argument. > > like this > > def some_function(req_var, req_var2, un_req_var): > do some stuff > return value > > how would i use this if sometimes i need to process un_req_var and > sometimes not ? def some_function(req_var, req_var2, un_req_var=None): do some stuff return value Now the caller can write some_function(1, 2) or some_function(1, 2, 3). You can distinguish the two by checking for 'un_req_var is None'. If None is a legitimate value for un_req_var then you need to pick some other sentinal value. If there is no built-in value that works, create your own: missing = object() def some_function(req_var, req_var2, un_req_var=missing): Kent _______________________________________________ Tutor maillist - Tutorpython.org http://mail.python.org/mailman/listinfo/tutor
[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )