Re: [Tutor] question about *args and functions
Great, gents, thanks. tried it out and is working fine, this will clean up a lot of stuff for me. thanks for your help ! shawn On 1/26/07, Wesley Brooks <[EMAIL PROTECTED]> wrote: 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 <[EMAIL PROTECTED]> 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 - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about *args and functions
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 <[EMAIL PROTECTED]> 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 - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about *args and functions
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 - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about *args and functions
2007/1/26, shawn bright <[EMAIL PROTECTED]>: 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. *args is used if there are arguments that could occur more than once. 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 ? Use: def some_function(req_var, req_var2, un_req_var = None): do some stuff return value Now, the function can be called both with and without un_req_var, and if it is called without un_req_var, within the function body un_req_var is considered to be None. -- Andre Engels, [EMAIL PROTECTED] ICQ: 6260644 -- Skype: a_engels ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] question about *args and functions
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 - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor