Last Update: 2013-4-9
重载此方法的,调用callable返回True,对于类来说,一直是callable的,但对于对象来说,如果没有实现__call__方法,则不是callable的,否则是callable的
>>>classShape(object):def __dir__(self):
return [’area’, ’perimeter’, ’location’]
>>>s=Shape()
>>>dir(s)
[’area’, ’perimeter’, ’location’]
class MyClass:
def __init__(self,value):
self.mValue = value
def __nonzero__(self):
if self.mValue % 2 :
return False
else:
return True
a1 = MyClass(1)
a2 = MyClass(2)
if a1:
print "a1 is not None" #a1为奇数,不输出
if a2:
print "a2 is not none" #a2为偶数,此句输出