
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How can I pass optional or keyword parameters from one function to another in Python?
To pass optional or keyword parameters from one function to another, collect the arguments using the * and ** specifiers in the function’s parameter list But, at first, do know what are *args and **args in Python. Let us understand them −
Variable-length/ Arbitrary arguments in Python (*args)
Example
When you don’t know in advance about the number of arguments to be passed, the arguments are variable-length. Include an asterisk i.e. * before the parameter name while defining the function. Let us see an example:
def demo(*car): print("Car 1 = ",car[0]) print("Car 2 = ",car[1]) print("Car 3 = ", car[2]) print("Car 4 = ", car[3]) # call demo("Tesla", "Audi", "BMW", "Toyota")
Output
('Car 1 = ', 'Tesla') ('Car 2 = ', 'Audi') ('Car 3 = ', 'BMW') ('Car 4 = ', 'Toyota')
Arbitrary Keyword Arguments in Python (**kwargs)
When you don’t know in advance about the number of keyword arguments to be passed, the arguments are arbitrary keyword arguments.
Example
Let us see an example −
def demo(**c): print("Car Name: "+c["name"]) print("Car Model: "+c["model"]) # call demo(name = "Tesla", model = "2022")
Output
Car Name: Tesla Car Model: 2022
Pass optional or keyword parameters from one function to another
To pass, collect the arguments using the * and ** in the function’s parameter list. Through this, you will get the positional arguments as a tuple and the keyword arguments as a dictionary. Pass these arguments when calling another function by using * and ** −
def f(a, *args, **kwargs): ... kwargs['width'] = '14.3c' ... g(a, *args, **kwargs)
- Related Articles
- How to pass optional parameters to a function in Python?
- How to pass keyword parameters to a function in Python?
- How can I declare optional function parameters in JavaScript?
- How to pass event objects from one function to another in JavaScript?
- How can I pass parameters in computed properties in VueJS?
- How to pass the parameters in the PowerShell function?
- How can I pass parameters to on_key in fig.canvas.mpl_connect('key_press_event',on_key)?
- How to pass values from one activity to another in Android?
- How can I sort one list by values from another list in Python?
- How do I pass an object from one activity to another on Android using Kotlin?
- How to pass data from one fragment to another fragment in android?
- How to pass multiple data from one activity to another in Android?
- How to pass a String from one Activity to another in Android?
- How to pass an object from one Activity to another in Android?
- How can I make one Python file run another?
