我在 lambda 模块中有一个函数 lambda_handler ,包括其他模块并调用 helloWorld 函数。
在 helloWorld 函数中传递参数或将变量作为全局变量不是一种选择。是否可以从早期范围访问变量?
#--- lambda.py ---
import my_module
def lambda_handler(event,context):
my_module.helloWorld()
#--- my_module.py ---
def helloWorld():
local_variable = <something>.context
使用inspect
模块获取调用帧的局部变量:
import inspect
def lambda_handler(event, context):
helloWorld()
def helloWorld():
calling_frame = inspect.currentframe().f_back
print(calling_frame.f_locals['event'])
print(calling_frame.f_locals['context'])
lambda_handler('an event', 'a context')
输出
an event
a context
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句