英文:
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):
然后 event
和 context
分别是 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论