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

你可能感兴趣的:(python标准库 _ _builtin_ _ 模块)