Call function twice or more in a loop

前端 未结 2 1432
予麋鹿
予麋鹿 2021-01-15 13:55

I use the code below with lambda to call function once in a loop, it works but now I am trying to call the function for specific times like 3 times in a loop, I

2条回答
  •  醉梦人生
    2021-01-15 14:40

    Another approach is to use function instead of class:

    def call_three_times(func, *args, **kwargs):
        def wrapper(*args, **kwargs):
            wrapper.called += 1
            return func(*args, **kwargs) if wrapper.called <= 3 else None
        wrapper.called = 0
        return wrapper
    
    
    @call_three_times
    def func():
        print "Called only three times"
    

提交回复
热议问题