python多函数methodcaller调用

```

from operator import methodcaller

class Job(object):

def __init__(self, jobid, inst, func):

self.id = jobid

self.inst = inst

self.func = str(func)

def __str__(self):

return "" % (self.id, self.inst)

class Pipeline(object):

def __init__(self):

self._jobs = []

def add_jobs(self, job):

self._jobs.append(job)

def run(self):

for job in self._jobs:

methodcaller(job.func)(job.inst)

```

使用方法:多个函数顺序执行时,可以使代码简洁,更加面对对象。

```

class A(object):

def a(object):

time.sleep(3)

print("aa")

class B(object):

def b(object):

time.sleep(2)

print("bb")

class C(object):

def c(object):

time.sleep(1)

print("cc")

a = A()

b = B()

c = C()

pipe = Pipeline()

step1 = Job(1, a, "a")

step2 = Job(2, b, "b")

step3 = Job(3, c, "c")

pipe.add_jobs(step1)

pipe.add_jobs(stpe2)

pipe.add_jobs(step3)

pipe.run()

```

你可能感兴趣的:(python语法,python,开发语言)