从隔离性、安全性和名称所有权角度审查这个生成的处理器工厂。
为每个租户创建定价处理器,并保存独立的尝试次数和租户专属折扣。
Python
attempts = 0
def build_handlers(tenants):
handlers = []
for tenant in tenants:
rate = tenant["discount"]
formula = tenant["formula"]
def handle(order):
global attempts
attempts += 1
if order["total"] <= 0:
return None
locals()["rate"] = rate
price = eval(formula)
return {
"tenant": tenant["name"],
"attempt": attempts,
"price": price,
}
handlers.append(handle)
return handlers
生成代码仅作示例,不代表任何特定模型