代码之家  ›  专栏  ›  技术社区  ›  sqram

导致Webpy内部服务器错误的if语句

  •  1
  • sqram  · 技术社区  · 15 年前

    我有这门课:

    class View(object):
        def main_page(self, extra_placeholders = None):
            file = '/media/Shared/sites/www/subdomains/pypular/static/layout.tmpl'
    
            placeholders = { 'site_name' : 'pypular' } 
    
            # If we passed placeholders vars, append them
            if extra_placeholders  != None:
                for k, v in extra_placeholders.iteritems():
                    placeholders[k] = v
    

    上面代码中的问题是if语句

    如您所见,函数接受一个参数(额外的占位符),它是一个dict。

    如果我不向main_page()传递参数,

    if extra_placeholders  == None:
        return 'i executed'
    

    运行良好。然而,

    if extra_placeholders  != None:
        return 'i cause error'
    

    不起作用。它会导致500个内部服务器错误。为什么?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Community CDub    8 年前

    你应该用它来代替吗

    if !( extra_placeholders  is  None) :
    

    编辑:反映评论:

    似乎(谢谢)您也可以使用:

     if extra_placeholders  is  not None :
    

    更新:原始链接现在已经死了,所以这个答案是一个很好的参考: https://stackoverflow.com/a/3289606/30225