python,apply関数

1188 ワード

#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