python函数参数执行

python函数参数执行,python,Python,各位, 在python中,在必要时才执行语句的正确方法是什么 假设我有一个函数执行指数api回退: def exponential_backoff_task(config, task, description): retry_count = config.get( 'retry_count', 5 ) api_backoff_initial_msec = config.get( 'api_backoff_initial_msec', 200) print 'Running

各位, 在python中,在必要时才执行语句的正确方法是什么

假设我有一个函数执行指数api回退:

def exponential_backoff_task(config, task, description):
    retry_count = config.get( 'retry_count', 5 )
    api_backoff_initial_msec = config.get( 'api_backoff_initial_msec', 200)
    print 'Running api-backoff task: %s, retry_count: %d, initial_backoff: %d' % (description, retry_count, api_backoff_initial_msec)
    result = None
    for r in range(retry_count):
        try:
            result = task
        except boto.exception.BotoServerError:
            delay_msec = (2 ** r) * api_backoff_initial_msec
            print 'Exponential backoff, retry %d for %d msecs...' % (r, delay_msec)
            time.sleep(delay_msec/1000)
            continue
        except:
            raise
    return result


def foo():
    all_instances = exponential_backoff_task(config, conn.get_all_dbinstances(), 'foo'))
在这种情况下,
conn.get\u all\u instances()
在调用函数时执行,而不是在
指数备份
函数中执行


谢谢

传递信息时不要打电话,只在需要时打电话:

from functools import partial

def exponential_backoff_task(config, task_fn, description):
    retry_count = config.get('retry_count', 5)
    api_backoff_initial_msec = config.get('api_backoff_initial_msec', 200)
    print 'Running api-backoff task: %s, retry_count: %d, initial_backoff: %d' % (description, retry_count, api_backoff_initial_msec)
    result = None
    for r in range(retry_count):
        try:
            # Call the function that got passed in
            result = task_fn()
        except boto.exception.BotoServerError:
            delay_msec = (2 ** r) * api_backoff_initial_msec
            print 'Exponential backoff, retry %d for %d msecs...' % (r, delay_msec)
            time.sleep(delay_msec / 1000)
            continue
        except:
            raise
    return result


def foo():
    # Note the missing parens: here you're just passing in the function
    all_instances = exponential_backoff_task(config, conn.get_all_dbinstances, 'foo')
编辑: 要在函数中预定义一些可以使用的参数,可以接收函数并将这些参数应用于函数,然后返回已应用这些参数的新函数,以下是一个示例:

from functools import partial

def f(a, b):
    print a
    print b

g = partial(f, a=1, b=2)

g()
这张照片

1
2

好吧,传递时不要打电话,只在需要时打电话:

from functools import partial

def exponential_backoff_task(config, task_fn, description):
    retry_count = config.get('retry_count', 5)
    api_backoff_initial_msec = config.get('api_backoff_initial_msec', 200)
    print 'Running api-backoff task: %s, retry_count: %d, initial_backoff: %d' % (description, retry_count, api_backoff_initial_msec)
    result = None
    for r in range(retry_count):
        try:
            # Call the function that got passed in
            result = task_fn()
        except boto.exception.BotoServerError:
            delay_msec = (2 ** r) * api_backoff_initial_msec
            print 'Exponential backoff, retry %d for %d msecs...' % (r, delay_msec)
            time.sleep(delay_msec / 1000)
            continue
        except:
            raise
    return result


def foo():
    # Note the missing parens: here you're just passing in the function
    all_instances = exponential_backoff_task(config, conn.get_all_dbinstances, 'foo')
编辑: 要在函数中预定义一些可以使用的参数,可以接收函数并将这些参数应用于函数,然后返回已应用这些参数的新函数,以下是一个示例:

from functools import partial

def f(a, b):
    print a
    print b

g = partial(f, a=1, b=2)

g()
这张照片

1
2

您甚至从未在方法中调用
task()
,您只需将其返回或
None
返回到
all\u实例
您甚至从未在方法中调用
task()
,您只需将其返回或
None
返回到
all_实例
如果希望
conn.get_all_dbinstances
用某些参数实例化该怎么办?如果希望
conn.get_all_dbinstances
用某些参数实例化该怎么办?