下面是代码
class Allquestion:
def __init__(self,question):
self.question=question
def show_question(self):
print(self.question)
my_new_question=Allquestion("english")
print(my_new_question.show_question())
输出结果
第一个是show_question 打印的english
第二个是print(my_new_question.show_question()) 由于你没有返回值,所以是none
print(my_new_question.show_question())
改为
my_new_question.show_question()
或者
print(self.question)改为return self.question
函数没有返回值但你打印返回值
第9行修改成
my_new_question.show_question()
这个函数内部已经调用了print进行打印,所以不需要外面再加一层print。因为my_new_question.show_question()没有设置return的返回值,所以返回了None, 在外面加了print,就是先调用my_new_question.show_question()打印了“english”, 然后调用外层的print打印里面函数返回的None.
python语言与其他语言不同,没有NULL类型,空用none来表示,但同时需要注意,none是有数据类型的,type为‘Nonetype’
因此python中判断对象为非空时需要注意对象类型
三种主要的写法有:
if X is None;
if not X;
当X为None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()这些时,not X为真(True),即无法分辨出他们之间的不同。
注意:
在Python中,None、空列表[]、空字典{}、空元组()、0等一系列代表空和无的对象会被转换成False。除此之外的其它对象都会被转化成True。
在命令if not 1中,1便会转换为bool类型的True。not是逻辑运算符非,not 1则恒为False。因此if语句if not 1之下的语句,永远不会执行。
lsit = []
if list:
print('非空列表')
else:
print('空')
例如:
type:str
判断语句:if val.strip() == ‘’:
例如:
数据库中看:数据值为null
这个其实就是个‘null’的字符串,并非空
判断语句:if val == ‘null’: