callable(object) : return true if the object appears callable, false if not. If the object is callable, it's still possible a call fails. If not, calling object will never succeed. Classes are callable, but class instances are not callable except that the class has a method __call__().
------------------------
Kind Regards,
Yonggang Meng
>>> callable(bool)
True
>>> callable(True)
False
>>> c =bool()
>>> c
False
>>> callable(c)
False
>>> class demo:
... def __call__(self):
... print 1
...
>>> callable(demo)
True
>>> d=demo()
>>> callable(d)
True
>>> class demo2:
... def __index__(self):
... print 2
...
>>> e=demo2()
>>> callable(e)
False
>>>
chr(i) : change an integer i to one character who ascii code is the integer, i must be in [0..255], Likely, unichr will change the integer to a unicode character.
On the opposite, ord(char) will change one character to an integer.
>>> chr(97)
'a'
>>> ord('a')
97
>>> ord("a")
97
>>> unichr(97)
u'a'
classmethod(function) ; like an attribute to decorate a function, the first argument must be an instance itself. It can be called dm.cm(2,3) or dm1.cm(2,3)
It's a little like extension method. If you use dm.cm(2,3), it will translate into dm.cm(dm(),2,3), if you use dm1.cm(2,3), it will translate into dm1.cm(dm1,2,3)
>>> class demo:
... @classmethod
... def cm(cls,arg1,arg2):
... print cls.v+arg1+arg2
... v=1
...
>>> demo.cm(2,4)
7
>>> d=demo()
>>> d.cm(2,3)
6
>>>
cmp(x,y) : compare the two objects x and y and return an integer, if x<y return negative value, if x==y return zero, else return positive value.
>>> cmp('a','b')
-1
>>> cmp('a','a')
0
>>> cmp('b','a')
1
>>>
Kind Regards,
Yonggang Meng
0 comments:
Post a Comment