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
Post a Comment