5.python 设计模式-函数里传参函数

传参

在函数传参中,传入数据

  • arg
    传入的参数值

  • func
    函数名

  • **kwargs
    func函数的传参

def func_a(arg, func, **kwargs):
    print(arg)
    func(**kwargs)


def B(arg_a):
    print(arg_a)


def C():
    print('Hello World')


if __name__ == '__main__':
    func_a(arg='arg', arg_a='Hello Python', func=B)
    func_a(arg='arg', func=C)

类中使用

类中使用函数调用,填写 self.函数名

class DatebaseAction(object):

    def sum_all_object(self):
        self.fetch_object(func=self.get_netwrok_object,limit=20,offset=0)

    @classmethod
    def fetch_object( self,func, **kwargs):
        '''
        :return:
        '''
        func(**kwargs)

    def get_netwrok_object(self,limit, offset):
        print('get_netwrok_object', limit, offset)

db= DatebaseAction()
db.sum_all_object()
posted @ 2021-01-27 16:04  鲁哒哒  阅读(59)  评论(0编辑  收藏  举报