说明:检查对象object是否可调用。如果返回True,object任然可能调用失败,但如果返回False,调用对象ojbect绝对不会成功。注意:类是可调用的,而类的实例实现了__call__()方法才可调用。
示例:
>>> callable(0)
False
>>> callable("mystring")
False
>>> def add(a, b):
… return a + b
…
>>> callable(add)
True
>>> class A:
… def method(self):
… return 0
…
>>> callable(A)
True
>>> a = A()
>>> callable(a)
False
>>> class B:
… def __call__(self):
… return 0
…
>>> callable(B)
True
>>> b = B()
>>> callable(b)
True
说明:返回整数i对应的ASCII字符。与ord()作用相反。
参数x:取值范围[0, 255]之间的正数。
示例:
>>> chr(97)
'a'
>>> ord('a')
97
说明:classmethod是用来指定一个类的方法为类方法,没有此参数指定的类的方法为实例方法,使用方法如下:
class C:
@classmethod
def f(cls, arg1, arg2, ...): ...
类方法既可以直接类调用(C.f()),也可以进行实例调用(C().f())。
示例:
>>> class C:
... @classmethod
... def f(self):
... print "This is a class method"
...
>>> C.f()
This is a class method
>>> c = C()
>>> c.f()
This is a class method
>>> class D:
... def f(self):
... print " This is not a class method "
...
>>> D.f()
Traceback (most recent call last):
File "", line 1, in
TypeError: unbound method f() must be called with D instance as first argument (got nothing instead)
>>> d = D()
>>> d.f()
This is not a class method
说明:比较两个对象x和y,如果x < y ,返回负数;x == y, 返回0;x > y,返回正数。
示例:
>>> cmp(1, 2)
-1
>>> cmp(3, 3)
0
>>> cmp(4, 3)
1
>>> cmp('abc','a')
1
compile(source, filename, mode[, flags[, dont_inherit]])
说明:将source编译为代码或者AST对象。代码对象能够通过exec语句来执行或者eval()进行求值。
参数source:字符串或者AST(Abstract Syntax Trees)对象。
参数 filename:代码文件名称,如果不是从文件读取代码则传递一些可辨认的值。
参数model:指定编译代码的种类。可以指定为 ‘exec’,’eval’,’single’。
参数flag和dont_inherit:这两个参数暂不介绍,碰到时再具体介绍。
示例:
>>> code = "for i in range(0, 10): print i"
>>> cmpcode = compile(code, '', 'exec')
>>> exec cmpcode
0
1
2
3
4
5
6
7
8
9
>>> str = "3 * 4 + 5"
>>> a = compile(str,'','eval')
>>> eval(a)
17
说明:创建一个值为real + imag * j的复数或者转化一个字符串或数为复数。如果第一个参数为字符串,则不需要指定第二个参数。
参数real: int, long, float或字符串;
参数imag: int, long, float。
示例:
>>> complex(3, 4)
(3 + 4j)
>>> complex(3)
(3 + 0j)
>>> complex("3")
(3 + 0j)
>>> complex("3 + 4j")
(3 + 4j)
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com