python标准库 _ _builtin_ _ 模块

这个模块包含 Python 中使用的内建函数. 一般不用手动导入这个模块;

Python 会帮你做好一切.



使用元组或字典中的参数调用函数  apply函数

apply函数和scala的apply函数功能类似

File: builtin-apply-example-1.py

def function(a, b):

    print a, b

apply(function, ("whither", "canada?"))

apply(function, (1, 2 + 3))

#whither canada? 1 5


File: builtin-apply-example-2.py

def function(a, b):

    print a, b

apply(function, ("crunchy", "frog"))

apply(function, ("crunchy",), {"b": "frog"})

apply(function, (), {"a": "crunchy", "b": "frog"})



crunchy frog

crunchy frog

crunchy frog


使用 apply 函数调用基类的构造函数

File: builtin-apply-example-3.py

class Rectangle:

    def _ _init_ _(self, color="white", width=10, height=10):

        print "create a", color, self, "sized", width, "x", height

class RoundedRectangle(Rectangle):

    def _ _init_ _(self, **kw):

        apply(Rectangle._ _init_ _, (self,), kw)

rect = Rectangle(color="green", height=100, width=100)

rect= RoundedRectangle(color="blue",height =20)


如果你写过较庞大的 Python 程序, 那么你就应该知道 import 语句是用来导

入外部模块的 (当然也可以使用 from-import 版本). 不过你可能不知道

import 其实是靠调用内建函数 _ _import_ _ 来工作的.

p15

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容