[英]In python is it possible to extract variables from a list inside a function call
I'm attempting to create a common create function that executes a function call to an SDK我正在尝试创建一个通用的创建函数来执行对 SDK 的函数调用
func: The SDK function is passed as my first parameter and the function itself can't be edited func: SDK函数作为我的第一个参数传递,函数本身无法编辑
request: This is a object that every create end point expects请求:这是每个创建端点都期望的对象
args: parameter list that will vary depending on the SDK function passed args:参数列表会根据传递的 SDK 函数而变化
SDK function signatures SDK函数签名
def create_a(self, create_type_a_details):
def create_b(self, extra_val_one create_type_b_details):
def create_c(self, extra_val_one, extra_val_two, create_type_c_details):
Current implementation当前实施
def create_resource(func, request, *args):
try:
if len(args) == 1:
response = func(args[0], request)
elif len(args) == 2:
response = func(args[0], args[1], request)
else:
response = func(request)
return response.data
except ServiceError as e:
log.error("Failed create operation {message}".format(message=e.message))
raise e
I don't like the above solution for obvious reasons.出于显而易见的原因,我不喜欢上述解决方案。 If there is another SDK create function that takes a third parameter before the request object, I will need to add a new elif.
如果有另一个 SDK 创建函数在请求对象之前接受第三个参数,我将需要添加一个新的 elif。
Is there a way I could call the SDK function with a dynamic set of parameters without editing the SDK functions to extract the values?有没有一种方法可以使用一组动态参数调用 SDK 函数,而无需编辑 SDK 函数来提取值? Can I use a Lamba inside of the function call?
我可以在函数调用中使用 Lamba 吗?
thanks Donagh谢谢多纳
You may just expand args
to give each element as a different parameter :您可以扩展
args
以将每个元素作为不同的参数:
def create_resource(func, request, *args):
response = func(*args, request)
return response.data
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.