*_ and *args assigns which value at runtime | python |

Not getting internally what is happening. *_ amd *args doing

what is assigned to default() at runtime

Using *_ in default

def default(*_):
  return "Not Happy"
a,b=10,20
default(a,b)

Using *args in default

def default(*args):
  return "Not Happy"
a,b=10,20
default(a,b)

I am trying to understand what is assigned to default at runtime. if not a and b

I have used website to see visually what happens at runtime during execution but not getting : https://pythontutor.com/visualize.html


In Python, you can define variadic functions (i.e. functions that can take any number of arguments), cf the tutorial or a RealPython article about it.

The gist of it is that you can declare a *whatever parameter, and Python will collect the variadic parameters and put them in there for you. For example :

>>> def foo(*args):
>>>     print(type(args), args)
>>>     
>>> foo()
<class 'tuple'> ()

>>> foo(1)
<class 'tuple'> (1,)

>>> foo(1, 'a')
<class 'tuple'> (1, 'a')

>>> foo(1, 'a', 'b', 'c')
<class 'tuple'> (1, 'a', 'b', 'c')

The convention is to call it args, but you can give it another name :

>>> def foo(*elephant):
>>>     print(elephant)
>>>     
>>> foo(1, 'a')
(1, 'a')

So when you call default(a,b), then *args (or *_ if you want) is filled, and you can use it in your function :

>>> def default(*args):
>>>     return "Not Happy" + str(args)
>>> 
>>> a,b=10,20
>>> default(a,b)
'Not Happy(10, 20)'

default is a function, when you do default(...) you call that function, meaning you execute the code you wrote in it. You are passing a and b, none of them is listed as a regular parameter, so both ends up in the *args parameter.