首页 新闻 搜索 专区 学院

请问Python中d与D的区别在什么地方呢

0
[已解决问题] 解决于 2012-06-01 17:08
# __nonzero__() overridden to return False
class D:
def __nonzero__(self):
return False
d = D()
print '__nonzero__() overridden to return False: ', bool(d)
print '__nonzero__() overridden to return False: ', bool(D)

为什么一个是True, 一个是False?

Hi, Shrek!的主页 Hi, Shrek! | 菜鸟二级 | 园豆:205
提问于:2012-02-23 20:28
< >
分享
最佳答案
0

object.__nonzero__(self)

Called to implement truth value testing and the built-in operation bool(); should return False or True, or their integer equivalents 0 or 1. When this method is not defined, __len__() is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither __len__() nor __nonzero__(), all its instances are considered true.

奖励园豆:5
NashZhou | 初学一级 |园豆:40 | 2012-04-27 08:45
其他回答(1)
0
dudu | 园豆:36992 (高人七级) | 2012-02-24 10:43

谢谢 dudu ,实际上关于__nonzero__和bool的用法我知道一二,最主要想知道,d=D(),这个有什么变化,这里的d和D有什么区别,谢谢

支持(0) 反对(0) Hi, Shrek! | 园豆:205 (菜鸟二级) | 2012-02-24 12:04
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册