[英]How to pass function arguments into a function inside the function
I'm trying to build a tool that assigns a value based on a method function argument that I choose in the assignment
function.我正在尝试构建一个工具,该工具根据我在
assignment
function 中选择的方法 function 参数分配值。 The problem is that the methods I use have different function arguments, ie assign_one
has n_groups
and assign_two
has direction
and distance
.问题是我使用的方法有不同的 function arguments,即
assign_one
有n_groups
和assign_two
有direction
和distance
。 Is there an elegant way to keep my assignment
function, but be able to pass in different function arguments to the functions I call inside (ie assign_one
and assign_two
)?有没有一种优雅的方法来保留我的
assignment
function,但能够将不同的 function arguments 传递给我在内部调用的函数(即assign_one
和assign_two
)?
Here is an example,这是一个例子,
def assign_one(df, n_groups):
...
def assign_two(df, direction, distance):
...
def assignment(grid, method)
if method == 'randomized':
grid['grid_id'] = assign_one(grid, n_groups)
if method == 'unique':
grid['grid_id'] = assign_two(grid, direction, distance)
assignment(grid, method='unique', n_groups=10)
assignment(grid, method='randomized', direction='diagonal', distance=50)
You can give your assignment()
function a **kwargs
parameter, that lets arbitrary keyword arguments be passed into it.你可以给你的
assignment()
function 一个**kwargs
参数,让任意关键字 arguments 传递给它。 kwargs
will then register inside assignment()
as an ordinary dict with key-value pairs, and you can take whichever keys you assume to have been passed, and pass them along to whichever method you need.然后,
kwargs
将在assignment()
中注册为具有键值对的普通 dict,您可以获取您认为已传递的任何键,并将它们传递给您需要的任何方法。
def assignment(grid, method, **kwargs)
if method == 'randomized':
grid['grid_id'] = assign_one(grid, kwargs.get('n_groups'))
if method == 'unique':
grid['grid_id'] = assign_two(grid, kwargs.get('direction'), kwargs.get('distance'))
I use .get()
instead of the standard square-bracket notation because .get()
doesn't immediately cause an error if the key isn't present (instead, it returns None
, or you can specify a different default value to return if it isn't there).我使用
.get()
而不是标准的方括号表示法,因为如果键不存在, .get()
不会立即导致错误(相反,它返回None
,或者您可以指定不同的默认值返回如果它不存在)。 Since kwargs
is for all intents and purposes a dict, you can decide what exactly you want to do with it yourself.由于
kwargs
出于所有意图和目的都是一个 dict,因此您可以自己决定您想用它做什么。
You can use partial application of assign_one
and assign_two
to create functions which have the same signature, that you can pass into assignment
, by using functools.partial
.您可以使用
assign_one
和assign_two
的部分应用程序来创建具有相同签名的函数,您可以使用functools.partial
将其传递给assignment
。
from functools import partial
def assign_one(df, n_groups):
...
def assign_two(df, direction, distance):
...
def assignment(grid, method):
grid['grid_id'] = method(grid)
assignment(grid, method=partial(assign_one, n_groups=10))
assignment(grid, method=partial(assign_two, direction='diagonal', distance=50))
If you want to keep the unique
, randomized
labels, you could also create a dictionary containing the partially applied functions:如果要保留
unique
的randomized
标签,还可以创建一个包含部分应用函数的字典:
methods = {
'unique': partial(assign_one, n_groups=10),
'randomized': partial(assign_two, direction='diagonal', distance=50)
}
def assignment(grid, method):
grid['grid_id'] = methods[method](grid)
assignment(grid, method='unique')
assignment(grid, method='randomized')
Or you could use a hybrid where you only store the original functions, but not the partially applied arguments, in the dictionary:或者,您可以使用只存储原始函数而不是部分应用的 arguments 的混合函数:
methods = {
'unique': assign_one,
'randomized': assign_two
}
def assignment(grid, method):
grid['grid_id'] = method(grid)
assignment(grid, partial(methods['unique'], n_groups=10))
assignment(grid, partial(methods['randomized'], direction='diagonal', distance=50))
You can use default arguments:您可以使用默认的 arguments:
def assignment(grid, method, n_groups=None, direction=None, distance=None):
# ... Your code
if direction is not None and distance is not None:
# Process randomized
def assign_one(df, n_groups):
...
def assign_two(df, direction, distance):
...
def assignment(grid, method, *args, **kwargs)
if method == 'randomized':
grid['grid_id'] = assign_one(grid, *args, **kwargs)
if method == 'unique':
grid['grid_id'] = assign_two(grid, *args, **kwargs)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.