关于 return s and s.strip() ......
Topic source谢谢!奇怪的是,s = None 的时候,s and s.strip() 不会报错,但是 s.strip() and s 会报错,真是有点不明白 and表达式 的机制了......
and 计算到第一个为假,就不会去计算第二个了。所以s and s.strip() 不会报错,因为不会调用s.strip()。但是 s.strip() and s 由于第一个调用的就是s.strip(),所以就报错了
- 1
谢谢!奇怪的是,s = None 的时候,s and s.strip() 不会报错,但是 s.strip() and s 会报错,真是有点不明白 and表达式 的机制了......
and 计算到第一个为假,就不会去计算第二个了。所以s and s.strip() 不会报错,因为不会调用s.strip()。但是 s.strip() and s 由于第一个调用的就是s.strip(),所以就报错了
laputa6616
有人能告诉我,删除空字符串那个例子里面为什么不直接用 return s.strip()呢,这里 return s and s.strip() 与 return s.strip() 有什么区别啊?