好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python快速教程(补充篇03):Python内置函数清单

Python内置(built-in)函数随着python解释器的运行而创建。在Python的程序中,你可以随时调用这些函数,不需要定义。最常见的内置函数是:

# define class

class Me(object):

def test(self):

print "Hello!"

def new_test():

print "New Hello!"

me = Me()

hasattr(me, “test”) # 检查me对象是否有test属性

getattr(me, “test”) # 返回test属性

setattr(me, “test”, new_test) # 将test属性设置为new_test

delattr(me, “test”) # 删除test属性

isinstance(me, Me) # me对象是否为Me类生成的对象 (一个instance)

issubclass(Me, object) # Me类是否为object类的子类

编译,执行

repr(me) # 返回对象的字符串表达

compile(“print(‘Hello’)”,’test.py’,'exec’) # 编译字符串成为code对象

eval(“1 + 1″) # 解释字符串表达式。参数也可以是compile()返回的code对象

exec(“print(‘Hello’)”) # 解释并执行字符串,print(‘Hello’)。参数也可以是compile()返回的code对象

其他

input(“Please input:”) # 等待输入

globals() # 返回全局命名空间,比如全局变量名,全局函数名

locals() # 返回局部命名空间

查看更多关于Python快速教程(补充篇03):Python内置函数清单的详细内容...

  阅读:43次