[Python3] Call by dynamically specifying the keyword argument of the function

0. Conclusion

f(arg1,arg2)Should be as follows


f(**{"arg1":hoge, "arg2":fuga})

1. What you want to do

Suppose you have the function f and the formal parameters of f are arg1 and arg2. If you specify only one of them, you can call it like f (arg1 =" hoge ") or f (arg2 =" fuga ").

At this time, I want to dynamically decide whether to specify arg1 or to specify arg2.

2. How to do

For example, "when you want to stochastically decide whether to call arg1 or arg2" can be done as follows.

python


import numpy as np
Probabilistic arguments= lambda: "arg1" if np.random.rand()> 0.5 else "arg2"

def f(arg1="(empty)", arg2="(empty)"):
    print("arg1 = "+str(arg1)+"\narg2 = "+str(arg2))

f(**{Probabilistic arguments():"fuga"})
#Half chance
#
# arg1 = fuga
# arg2 = (empty)
#
#Is displayed,Another half chance
#
# arg1 = (empty)
# arg2 = fuga
#
#Is displayed

Recommended Posts

[Python3] Call by dynamically specifying the keyword argument of the function
Expansion by argument of python dictionary
[Python3] Rewrite the code object of the function
Pandas of the beginner, by the beginner, for the beginner [Python]
Get the caller of a function in Python
Sort the elements of the array by specifying the conditions
Specifying the range of ruby and python arrays
Fix the argument of the function used in map
[Python numpy] Dynamically specify the index of the array
About the * (asterisk) argument of python (and itertools.starmap)
the zen of Python
Python function argument summary
Find the cumulative distribution function by sorting (Python version)
[Python of Hikari-] Chapter 06-02 Function (argument and return value 1)
Python3 datetime is faster just by specifying the timezone
Today's python error: invalid keyword argument for this function
I examined the argument class_weight of Chainer's softmax_cross_entropy function.
Why is the first argument of [Python] Class self?
Sort tuple list in Python by specifying the ascending / descending order of multiple keys
Add a function to tell the weather of today to slack bot (made by python)
Try to get the function list of Python> os package
Towards the retirement of Python2
[python] Value of function object (?)
[Python] Etymology of python function names
About the ease of Python
Have the equation graph of the linear function drawn in Python
Check the scope of local variables with the Python locals function.
__init__ called by wxPython or Tkinter was a __init__ call of the inheriting class in Python
Keyword extraction by MeCab (python)
Debug by attaching to the Python process of the SSH destination
Find the diameter of the graph by breadth-first search (Python memory)
Call the API with python3.
Dynamically call methods in Python
About the features of Python
2015-11-26 python> Display the function list of the module> import math> dir (math)
The Power of Pandas: Python
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.
Be careful when specifying the default argument value in Python3 series
[Python] Display only the elements of the list side by side [Vertical, horizontal]
[Understanding in the figure] Management of Python virtual environment by Pipenv
Try transcribing the probability mass function of the binomial distribution in Python
If you give a list with the default argument of the function ...
Read the standard output of a subprocess line by line in Python
[Python of Hikari-] Chapter 07-02 Exception handling (continuous execution of the program by exception handling)
A function that measures the processing time of a method in python
[Python3] Define a decorator to measure the execution time of a function
[Python] A simple function to find the center coordinates of a circle
Sort of tuple array can be accelerated by specifying key (Python)
Call a Python function from p5.js.
The story of Python and the story of NaN
[python] Callback function (pass function as argument)
Function argument type definition in python
First Python 3 ~ The beginning of repetition ~
Existence from the viewpoint of Python
pyenv-change the python version of virtualenv
Change the Python version of Homebrew
[Python] Make the function a lambda function
[Python] Understanding the potential_field_planning of Python Robotics
Review of the basics of Python (FizzBuzz)
Behavior of python3 by Sakura's server
Basic Python operation 2nd: Function (argument)