首页 > 文章列表 > python none代表什么

python none代表什么

Python none
196 2022-08-07

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

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

1、assert断言:

mylist = ['a', 'b', 'c']
>>> assert len(mylist) is not None # 用assert判断列表不为空,正确无返回
>>> assert len(mylist) is None # 用assert判断列表为空

相关推荐:《Python入门教程》

2、if...else...

a = None
if a:
    print "a is not None"
else:
    print "a is None"

3、如果函数无return,则默认返回None

def 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