Is it good practice to add optional arguments to AWS lambda function?

huangapple go评论55阅读模式
英文:

Is it good practice to add optional arguments to AWS lambda function?

问题

例如,对于一个Python AWS Lambda,文档中的签名是:

def handler(event, context):

但以下内容也是有效的Python语法:

def handler(event, context, *args, **kwargs):

我已经测试过这样做,Lambda不会崩溃。(顺便说一下,默认参数也是有效的)

这允许例如装饰处理函数,然后向函数引入额外的参数。考虑一下依赖注入的情况。

在任何情况下,改变默认签名是否被认为是不良做法,如果是的话,以何种方式?

英文:

For example, a python AWS lambda, the documented signature is:

def handler(event, context):

But the following is valid python syntax:

def handler(event, context, *args, **kwargs):

I've tested this and the lambda does not crashes. (BTW default arguments is also valid)

This would allow, for example, to decorate the handler function and then introduce extra arguments to the function. Think about a dependency injection scenario for example.

Is 'altering' the default signature considered bad practice in any way, and if so, in which way?

答案1

得分: 1

这完全取决于您,甚至可以这样做:

def lambda_handler(*args, **kwargs):

然后 eventcontext 分别是 args[0]args[1]

Lambda 只关心 Lambda 处理程序函数至少接受两个位置参数。

英文:

This is entirely up to you, you could even do:

def lambda_handler(*args, **kwargs):

with event and context then being args[0] and args[1] respectively.

Lambda only cares that the lambda handler function takes at least two positional arguments.

huangapple
  • 本文由 发表于 2023年5月18日 13:54:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76278086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定