Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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')
Comment

Using **kwargs to pass the variable keyword arguments to the function

# Welcome to softhunt.net
def intro(**data):
    print("
Data type of argument:",type(data))

    for key, value in data.items():
        print("{} is {}".format(key,value))

intro(Firstname="Ranjeet", Lastname="Kumar", Age=22, Phone=1122334455)
intro(Firstname="Softhunt", Lastname=".net", Email="softhunt.net@gmail.com", Country="Pakistan", Age=22, Phone=1122334455)
Comment

PREVIOUS NEXT
Code Example
Python :: Combining functions 
Python :: keyword argument python 
Python :: tree view width 
Python :: basic kivy md 
Python :: python set vs tuple performance 
Python :: python to uml 
Python :: bulet in jupyter notebook 
Python :: Getting TimeZone from lat long coordinate 
Python :: get complete path from reletive path python 
Python :: python typing optional argument 
Python :: find a paragraph in requests-html 
Python :: Python NumPy atleast_3d Function Syntax 
Python :: Python NumPy Shape function example verifying the value of last dimension 
Python :: conmbination in python 
Python :: how to convrete .npz file to txt file in python 
Python :: Python NumPy block Function Example by using np.eye function 
Python :: tensorflow configure multiple gpu 
Python :: python increase a value every n rows 
Python :: Python how to use __div__ 
Python :: python mxs Classof 
Python :: Create a list of multiples of 3 from 0 to 20. 
Python :: drop column 0 to many 
Python :: jenkins crumb python request 
Python :: Fatal Python error: Cannot recover from stack overflow. 
Python :: make dialog in the front by Pywinauto 
Python :: python extract multiple values from a single cell in a dataframe column using pandas 
Python :: matrix implement 
Python :: python 3.9.7 
Python :: singke line expresions python 
Python :: python go back one using abspath 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =