使关键字参数默认为变量名的方法?python
是否可以将函数的关键字参数默认定义为变量名?我之所以想这样做,是因为我发现自己正在创建包含大量关键字参数的函数,但我希望这些关键字参数默认为我在使用函数之前定义的变量名。我也乐于接受解决方案,这些解决方案允许我以其他方式解决重复性问题。这只是我一直在思考的一个想法 例如,我想做这样的事情使关键字参数默认为变量名的方法?python,python,Python,是否可以将函数的关键字参数默认定义为变量名?我之所以想这样做,是因为我发现自己正在创建包含大量关键字参数的函数,但我希望这些关键字参数默认为我在使用函数之前定义的变量名。我也乐于接受解决方案,这些解决方案允许我以其他方式解决重复性问题。这只是我一直在思考的一个想法 例如,我想做这样的事情 def do_something(x=x, y=y, z=z, t=t): return x, y 我知道上面返回了一个错误,因为我还没有定义x 我懒得经常写: x = 100 y= 50 z= 10
def do_something(x=x, y=y, z=z, t=t):
return x, y
我知道上面返回了一个错误,因为我还没有定义x
我懒得经常写:
x = 100
y= 50
z= 10
t= 1
do_something(x=x, y=y, z=z, t=t)
我宁愿使用已经假定为变量名的默认值来编写:
x = 100
y= 50
z= 10
t= 1
do something()
Python允许您传入一个字典,并告诉函数将其解包
args = dict(x=100, y=50, z=5, t=1)
def do_something(args) # star tells the function to unpack as dict the variables you made
do_something(**args) # call it like this
您甚至可以将其作为带有一颗星的列表传递:
args = [100, 50, 5, 1]
def do_something(x,y,z,t) # star tells the function to unpack the variables you made
do_something(*args) # call it like this
Python还允许默认参数:
do_something(x=100, y=50, z=5, t=1) #if you don't pass arguments, it will use these defaults
Python允许您传入一个字典,并告诉函数将其解包
args = dict(x=100, y=50, z=5, t=1)
def do_something(args) # star tells the function to unpack as dict the variables you made
do_something(**args) # call it like this
您甚至可以将其作为带有一颗星的列表传递:
args = [100, 50, 5, 1]
def do_something(x,y,z,t) # star tells the function to unpack the variables you made
do_something(*args) # call it like this
Python还允许默认参数:
do_something(x=100, y=50, z=5, t=1) #if you don't pass arguments, it will use these defaults
Python允许您传入一个字典,并告诉函数将其解包
args = dict(x=100, y=50, z=5, t=1)
def do_something(args) # star tells the function to unpack as dict the variables you made
do_something(**args) # call it like this
您甚至可以将其作为带有一颗星的列表传递:
args = [100, 50, 5, 1]
def do_something(x,y,z,t) # star tells the function to unpack the variables you made
do_something(*args) # call it like this
Python还允许默认参数:
do_something(x=100, y=50, z=5, t=1) #if you don't pass arguments, it will use these defaults
Python允许您传入一个字典,并告诉函数将其解包
args = dict(x=100, y=50, z=5, t=1)
def do_something(args) # star tells the function to unpack as dict the variables you made
do_something(**args) # call it like this
您甚至可以将其作为带有一颗星的列表传递:
args = [100, 50, 5, 1]
def do_something(x,y,z,t) # star tells the function to unpack the variables you made
do_something(*args) # call it like this
Python还允许默认参数:
do_something(x=100, y=50, z=5, t=1) #if you don't pass arguments, it will use these defaults
最简单的方法是使用字典而不是变量:
def do_something(x, y, z, t):
...
data = dict(
x = 100,
y = 50,
z = 10,
t = 1
)
do_something(**data)
这里有一个非常简单的方法:
import inspect
def autofill(fn):
def _autofill(*args, **kwargs):
kwargs.update({arg:globals()[arg] for arg in inspect.getargspec(fn).args if arg not in kwargs})
return fn(*args, **kwargs)
return _autofill
>>> @autofill
... def f(x,y):
... return x,y
...
>>> x = 1; y = 20
>>> f()
(1, 20)
>>> f(y=2)
(1, 2)
最简单的方法是使用字典而不是变量:
def do_something(x, y, z, t):
...
data = dict(
x = 100,
y = 50,
z = 10,
t = 1
)
do_something(**data)
这里有一个非常简单的方法:
import inspect
def autofill(fn):
def _autofill(*args, **kwargs):
kwargs.update({arg:globals()[arg] for arg in inspect.getargspec(fn).args if arg not in kwargs})
return fn(*args, **kwargs)
return _autofill
>>> @autofill
... def f(x,y):
... return x,y
...
>>> x = 1; y = 20
>>> f()
(1, 20)
>>> f(y=2)
(1, 2)
最简单的方法是使用字典而不是变量:
def do_something(x, y, z, t):
...
data = dict(
x = 100,
y = 50,
z = 10,
t = 1
)
do_something(**data)
这里有一个非常简单的方法:
import inspect
def autofill(fn):
def _autofill(*args, **kwargs):
kwargs.update({arg:globals()[arg] for arg in inspect.getargspec(fn).args if arg not in kwargs})
return fn(*args, **kwargs)
return _autofill
>>> @autofill
... def f(x,y):
... return x,y
...
>>> x = 1; y = 20
>>> f()
(1, 20)
>>> f(y=2)
(1, 2)
最简单的方法是使用字典而不是变量:
def do_something(x, y, z, t):
...
data = dict(
x = 100,
y = 50,
z = 10,
t = 1
)
do_something(**data)
这里有一个非常简单的方法:
import inspect
def autofill(fn):
def _autofill(*args, **kwargs):
kwargs.update({arg:globals()[arg] for arg in inspect.getargspec(fn).args if arg not in kwargs})
return fn(*args, **kwargs)
return _autofill
>>> @autofill
... def f(x,y):
... return x,y
...
>>> x = 1; y = 20
>>> f()
(1, 20)
>>> f(y=2)
(1, 2)
您是否检查了
functools
模块
from functools import partial
def do_something(x,y,z,t):
print(x,y,z,t)
do_something = partial(do_something, x=100, y=50, z=10, t=1)
do_something()
do_something(x=30)
您是否检查了
functools
模块
from functools import partial
def do_something(x,y,z,t):
print(x,y,z,t)
do_something = partial(do_something, x=100, y=50, z=10, t=1)
do_something()
do_something(x=30)
您是否检查了
functools
模块
from functools import partial
def do_something(x,y,z,t):
print(x,y,z,t)
do_something = partial(do_something, x=100, y=50, z=10, t=1)
do_something()
do_something(x=30)
您是否检查了
functools
模块
from functools import partial
def do_something(x,y,z,t):
print(x,y,z,t)
do_something = partial(do_something, x=100, y=50, z=10, t=1)
do_something()
do_something(x=30)
你的上一个例子有什么问题吗?
有什么问题吗?(x=100,y=50,z=10,t=1)
?或者,见鬼,做点什么(100,50,10,1)
?你懒得写的东西似乎不是你必须写的东西。如果你这么做,函数的行为将取决于你调用函数时这些全局变量处于什么状态。如果这些变量发生了变化,而您在调用函数时忘记了考虑这些变量,您将得到错误的输出,这就是为什么人们通常不推荐它的原因。@Marius,那不是真的。函数运行时,值是固定的defined@linuts:是的,对不起。为了清楚起见,您可以编写一个与OP上一个示例/所需行为相同的函数,但它将受制于我描述的问题。上一个示例有什么问题吗?有什么问题吗(x=100,y=50,z=10,t=1)
?或者,见鬼,做点什么(100,50,10,1)
?你懒得写的东西似乎不是你必须写的东西。如果你这么做,函数的行为将取决于你调用函数时这些全局变量处于什么状态。如果这些变量发生了变化,而您在调用函数时忘记了考虑这些变量,您将得到错误的输出,这就是为什么人们通常不推荐它的原因。@Marius,那不是真的。函数运行时,值是固定的defined@linuts:是的,对不起。为了清楚起见,您可以编写一个与OP上一个示例/所需行为相同的函数,但它将受制于我描述的问题。上一个示例有什么问题吗?有什么问题吗(x=100,y=50,z=10,t=1)
?或者,见鬼,做点什么(100,50,10,1)
?你懒得写的东西似乎不是你必须写的东西。如果你这么做,函数的行为将取决于你调用函数时这些全局变量处于什么状态。如果这些变量发生了变化,而您在调用函数时忘记了考虑这些变量,您将得到错误的输出,这就是为什么人们通常不推荐它的原因。@Marius,那不是真的。函数运行时,值是固定的defined@linuts:是的,对不起。为了清楚起见,您可以编写一个与OP上一个示例/所需行为相同的函数,但它将受制于我描述的问题。上一个示例有什么问题吗?有什么问题吗(x=100,y=50,z=10,t=1)
?或者,见鬼,做点什么(100,50,10,1)
?你懒得写的东西似乎不是你必须写的东西。如果你这么做,函数的行为将取决于你调用函数时这些全局变量处于什么状态。如果这些变量发生了变化,而您在调用函数时忘记了考虑这些变量,您将得到错误的输出,这就是为什么人们通常不推荐它的原因。@Marius,那不是真的。函数运行时,值是固定的defined@linuts:是的,对不起。为了清楚起见,您可以编写一个与OP上一个示例/所需行为相同的函数,但它会受到我描述的问题的影响。哦,这也很有趣。我没听说过这个。现在,为了简单起见,我将坚持使用字典,但将来我可能会尝试使用它。哦,这也很有趣。我没听说过这个。现在,为了简单起见,我将坚持使用字典,但将来我可能会尝试使用它。哦,这也很有趣。我没听说过这个。现在,为了简单起见,我将坚持使用字典,但将来我可能会尝试使用它。哦,这也很有趣。我没听说过这个。现在,我将继续使用sim卡字典