Python using *args with default argument -


python newb here. suppose have function

def myfunc(a = "apple", *args):     print(a)     b in args:         print(b + "!") 

how pass set of unnamed arguments *args without altering default argument?

what want is

myfunc(,"banana", "orange") 

and output

apple banana! orange! 

but doesn't work.

(i'm sure has been discussed before searching came empty)

in python 3 can changing a keyword argument:

>>> def myfunc(*args, a="apple"):         print(a)         b in args:             print(b + "!") ...          >>> myfunc("banana", "orange") apple banana! orange! >>> myfunc("banana", "orange", a="watermelon") watermelon banana! orange! 

in python 2 you'll have this:

>>> def myfunc(*args, **kwargs):         = kwargs.pop('a', 'apple')         print          b in args:              print b + "!" 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -