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

时间: 2024-08-29 15:11:15

insert 和 if x is not None的相关文章

专利事务所信息Python爬取

数据来源:http://www.acpaa.cn/ 目前事务所的信息没有做反爬限制,还是很容易拿到数据的 没有用html解析工具,直接上正则,结果就是需要处理很多乱七八糟的空格...为了能将日期顺利的插入到数据库,做了很多转换.这个代码没用多线程. 下面是代码,Python版本为3.5,需要安装pymsql,mysql # -*- coding: UTF-8 -*- import http.client import re import pymysql def saveAgency(code,

【Leetcode】Insert Delete GetRandom O(1) - Duplicates allowed

题目链接:https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/ 题目: Design a data structure that supports all following operations in average O(1) time. Note: Duplicate elements are allowed. insert(val): Inserts an item val to the c

Bulk Insert Syntax

BULK INSERT Imports a data file into a database table or view in a user-specified format. BULK INSERT [ database_name . [ schema_name ] . | schema_name . ] [ table_name | view_name ] FROM 'data_file' [ WITH ( [ [ , ] BATCHSIZE = batch_size ] [ [ , ]

实战c++中的vector系列--再谈vector的insert()方法(都是make_move_iterator惹的祸)

之前说过了关于vector的insert()方法,把vector B的元素插入到vector A中,vector A中的结果我们可想而知,但是vector B中的元素还会如何? 看看之前写过的程序: #include <iostream> #include <vector> int main () { std::vector<int> myvector (3,100); std::vector<int>::iterator it; it = myvector

LeetCode Insert Interval

原题链接在这里:https://leetcode.com/problems/insert-interval/ AC Java: 1 /** 2 * Definition for an interval. 3 * public class Interval { 4 * int start; 5 * int end; 6 * Interval() { start = 0; end = 0; } 7 * Interval(int s, int e) { start = s; end = e; } 8

Java MongoDB insert

public class  Foo implements IdObject { public String id;    public String firstName;    private String lastName;        @Override     String getId() {        return id;    }        @Override    public void toDocument(Document doc) {        doc.appen

【MongoDB学习笔记6】深入MongoDB的创建/插入(insert)

简单单个文档插入用insert方法: > db.post.insert({"bar":"baz"});     WriteResult({ "nInserted" : 1 }) 批量插入,用insert方法(参数要是一个文档数组): > db.post.insert([{"_id":0},{"_id":1},{"_id":2}]);    BulkWriteResult({ 

【MongoDB】The basic operation of Mongodb, Insert\Query\Delete\Update

1, Insert MongoDB is database storing document object, the type of which is called Bson.(like JSON); Example:  // document defination Now after using command[db.posts.insert(doc)], you will insert record successfully if seeing the The following pictu

veridata实验举例(3)验证veridata查找出insert操作导致的不同步现象

veridata实验举例(3)验证veridata查找出insert操作导致的不同步现象 续接:<veridata实验举例(2)验证表BONUS与表SALGRADE两节点同步情况>,地址:点击打开链接 环境: Item Source System Target System Platform Red Hat Enterprise Linux Server release 5.4 Red Hat Enterprise Linux Server release 5.4 Hostname gc1 g

mongodb3.2系统性学习——1、文档插入insert insertOne insertMany

写操作——添加操作 mongodb提供以下操作执行添加文档操作 db.collection.insertOne() 3.2新添加 db.collection.insertMany() 3.2 新添加 db.collection.insert() 首先介绍下 insertone() 操作 语法规则: db.collection.insertOne( <document>, { writeConcern: <document> //Optional. A document expres