python中None代表一个特殊的空值,即为一个空对象,没有任何的值。

一般用于assert,判断,函数无返回时的默认,具体如下:

1、assert断言:mylist = ['a', 'b', 'c']

>>> assert len(mylist) is not None # 用assert判断列表不为空,正确无返回

>>> assert len(mylist) is None # 用assert判断列表为空

2、if...else...a = None

if a:

print "a is not None"

else:

print "a is None"

3、如果函数无return,则默认返回Nonedef add1(a,b):

return a+b

a1=add1(1,2)

print a1

#会输出3,因为有return,则有返回值

def add2(a,b):

print a+b

a2 = add2(1,2)

print a2

#会输出None,因为没有return,则add2为None

更多推荐

python里none什么意思_python none代表什么