How to provide default value for *args and **kwargs? (Python) | Sololearn: Learn to code for FREE!

0

How to provide default value for *args and **kwargs? (Python)

10/11/2021 8:31:09 AM

I'm A Baked Potato

11 Answers

New Answer

+3

def f(*li,**di): li = li or [1,2] di = di or {"a":5,"b":6}

+3

There isn't a default value, can you try explaining what you mean?

+2

*args is for an iterable and *kwargs is for a dictionary

+2

No, just add the defaults in before hand. The point of *args and *kwargs is to handle a changable amount of values. If you have set values it defeats the purpose.

+1

I Think You Mean To Say Values..... Values Are Used As: def function(arg1, arg2, arg3): # Code... This Function Will Only Allow 3 Arguments Not More Than That Nor Less Than That...

+1

Slick Like this.. def my_func(y=7, *args, **kwargs): print(y) my_func () # 7 my_func (10) # 10 #But how to do same with *args and **args?

+1

Arsalan interesting!

+1

Shubham Bhatia , The Answer You POSTED Is Actually A Python Program... C Doesn't Use def To Create A Function!

0

Slick Yes but can't I provide it a default iterable for *args and default keywords for **kwargs same way I did with formal parameter?

0

Slick Okay Thank you!

-3

def function(arg1, arg2, arg3) : #Python Coding #AngularJS developer. This is C language Program. 💯