cfn-lambda-handler icon indicating copy to clipboard operation
cfn-lambda-handler copied to clipboard

using [] and {} as default param value

Open zhaowb opened this issue 6 years ago • 2 comments

It's generally not encouraged to use [] and {} as function param default value because they are prone to cause unexpected modifications if the default value is used and modified in the function. The best way is to declare param=None then in the start of the function check if param is None then let param = [] (or {})

In cfn_lambda_handler/cfn_lambda_handler.py there are 3 places of such case,

  1. CfnLambdaExecutionTimeout.__init__
  2. cfn_handler
  3. Handler.__init__

Currently the code doesn't modify these params so there is no bug but I feel it's better to change these cases to prevent bugs if future changes modify these params.

zhaowb avatar Apr 28 '19 09:04 zhaowb

I think the better explanation is that default arguments are only evaluated once so subject to unexpected behaviour if the function is called once again with defaults.

But yes I agree :)

mixja avatar Apr 28 '19 09:04 mixja

Yes your explanation is clearer. Actually, a better way to fix is to use () to replace [] until maybe in the future the param needs to be updated somehow (very unlikely)...

zhaowb avatar Apr 29 '19 02:04 zhaowb