这种使用python三元运算符失败的原因不清楚?

2024-05-01 22:05:47 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的函数中,如果存在一个字典(作为参数传入),则向它添加另一个字典,或者将它用作字典。(此处为相关词典)

def some_view(request, form_class, template, success_url, context=None):
    ..........
    if context is not None:
        context.update({'form': form})
    else:
        context = {'form': form}
    return render(request, template, context)

这很好,但使用

context = context.update({'form': form}) if context is not None else {'form': form}

由于某种原因失败,因为上下文返回为None?你知道吗


Tags: 函数formnone参数if字典isrequest