您的位置:首页 > 编程语言 > Python开发

廖雪峰Python教程疑问

2016-08-25 09:06 344 查看
1. 字符串的strip()方法

原文:

“把一个序列中的空字符串删掉,可以这么写:

def not_empty(s):


   return s and s.strip()


list(filter(not_empty, ['A', '', 'B', None, 'C', ' ']))


# 结果: ['A', 'B', 'C']”


疑问:如果改为,

def not_empty(s):


   return s.strip()


就会提示错误,AttributeError: 'Nonetype' object has no attribute 'strip'

不知道 s and s.strip() 与 s.strip()的区别在哪里。

出处:http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431821084171d2e0f22e7cc24305ae03aa0214d0ef29000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: