tonwei139 发表于 2017-4-26 08:47:38

python,apply函数

  # 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
页: [1]
查看完整版本: python,apply函数