当前位置:网站首页>Problem solving: attributeerror: 'nonetype' object has no attribute 'append‘

Problem solving: attributeerror: 'nonetype' object has no attribute 'append‘

2022-07-05 02:22:00 Gongzicong

python Medium append() Method is used for Add a new object at the end of the list .
The method No return value ( That is, the return value is null ), however Will modify the original list .

a = [1, 2, 3]
b = a.append(4)
print(a)        # [1, 2, 3, 4]
print(b)        # None

It can be seen from the result execution ,append() no return value (b The value of is empty ), So if b Add a new object on , namely :

b.append(5)

An error will be reported after execution   AttributeError: 'NoneType' object has no attribute 'append'

because b It's an empty type , Not a list type , So there was no append() Method , Here should be b.append(5) It is amended as follows a.append(5)


Summary :
append() Method is used to add a new object at the end of the list , no return value , But it changes the original list .
therefore , In the use of append() When the method is used , You cannot assign its execution result to other variables before viewing or other operations .

Reference material :
https://www.runoob.com/python/att-list-append.html
https://blog.csdn.net/weixin_44249185/article/details/102580621

原网站

版权声明
本文为[Gongzicong]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050220228177.html