论坛首页 编程语言技术论坛

python,apply函数

浏览 2575 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-07-31  

# apply函数的使用

def function(a,b):
    print(a,b)
apply(function,('good','better'))
apply(function,(2,3+6))
apply(function,('cai','quan'))
apply(function,('cai',),{'b':'caiquan'})
apply(function,(),{'a':'caiquan','b':'Tom'})
#--使用 apply 函数调用基类的构造函数
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)

 输出结果:

('good', 'better')
(2, 9)
('cai', 'quan')
('cai', 'caiquan')
('caiquan', 'Tom')
create a green <__main__.Rectangle instance at 0x0678FA08> sized 100 x 100
create a blue <__main__.RoundedRectangle instance at 0x06620468> sized 10 x 20

 

论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics