zoukankan      html  css  js  c++  java
  • insert 和 if x is not None

    insert(位置,元素)

    #!/usr/bin/python
    
    aList = [123, 'xyz', 'zara', 'abc']
    
    aList.insert( 3, 2009)
    
    print "Final List : ", aList

    也许你是想判断x是否为None,但是却把`x==[]`的情况也判断进来了,此种情况下将无法区分。

    对于习惯于使用if not x这种写法的pythoner,必须清楚x等于None,  False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()时对你的判断没有影响才行。 

    而对于`if x is not None`和`if not x is None`写法,很明显前者更清晰,而后者有可能使读者误解为`if (not x) is None`,因此推荐前者,同时这也是谷歌推荐的风格

    结论:

    `if x is not None`是最好的写法,清晰,不会出现错误,以后坚持使用这种写法。

    使用if not x这种写法的前提是:必须清楚x等于None,  False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()时对你的判断没有影响才行。

     

    dd

  • 相关阅读:
    弱网环境测试点总结
    【CMDB】高级配置
    【CMDB】获取服务器数据
    Centos部属前后端项目
    Centos部署项目
    Django
    nginx反向代理和负载均衡
    nginx的配置
    centos7 安装nginx
    centos7 安装Virtualenv
  • 原文地址:https://www.cnblogs.com/hanggegege/p/5842481.html
Copyright © 2011-2022 走看看