0

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

11th Oct 2021, 8:31 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
11 Respostas
+ 4
def f(*li,**di): li = li or [1,2] di = di or {"a":5,"b":6}
11th Oct 2021, 11:17 AM
Arsalan [Inactive]
Arsalan [Inactive] - avatar
+ 3
There isn't a default value, can you try explaining what you mean?
11th Oct 2021, 8:34 AM
Slick
Slick - avatar
+ 2
*args is for an iterable and *kwargs is for a dictionary
11th Oct 2021, 8:43 AM
Slick
Slick - avatar
+ 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.
11th Oct 2021, 8:56 AM
Slick
Slick - avatar
+ 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...
11th Oct 2021, 8:36 AM
Sancho Godinho
Sancho Godinho - avatar
+ 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?
11th Oct 2021, 8:41 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
+ 1
Arsalan interesting!
11th Oct 2021, 12:17 PM
I Am a Baked Potato
I Am a Baked Potato - avatar
+ 1
Shubham Bhatia , The Answer You POSTED Is Actually A Python Program... C Doesn't Use def To Create A Function!
12th Oct 2021, 2:39 AM
Sancho Godinho
Sancho Godinho - avatar
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?
11th Oct 2021, 8:44 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
0
Slick Okay Thank you!
11th Oct 2021, 9:02 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
- 3
def function(arg1, arg2, arg3) : #Python Coding #AngularJS developer. This is C language Program. 💯
11th Oct 2021, 11:48 PM
Shubham Bhatia
Shubham Bhatia - avatar