python *args and **kwargs
def foo(*args):
for a in args:
print(a)
foo(1)
# 1
foo(1,2,3)
# 1
# 2
# 3
python *args and **kwargs
def foo(*args):
for a in args:
print(a)
foo(1)
# 1
foo(1,2,3)
# 1
# 2
# 3
pass args and kwargs to funcitons
#1. When used as part of a function definition
def f(self, *args, **kwargs):
#it is used to signify an arbitrary number of positional or
#keyword arguments, respectively.
#The point to remember is that inside the function args will be a tuple,
#and kwargs will be a dict.
#2. When used as part of a function call,
args = (1, 2)
kwargs = {'last': 'Doe', 'first': 'John'}
self.f(*args, **kwargs)
#the * and ** act as unpacking operators.
#args must be an iterable, and kwargs must be dict-like.
#The items in args will be unpacked and sent to the function
#as positional arguments, and the key/value pairs in kwargs
#will be sent to the function as keyword arguments.
#Thus,
self.f(*args, **kwargs)
#is equivalent to
self.f(1, 2, last='Doe', first='John')
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us