简体   繁体   中英

python function parameter execution

Folks, What is the proper way in python not to execute a statement until necessary.

Lets say I have a function that does exponential api backoff:

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'))

In this case, conn.get_all_instances() gets executed when a function is called, instead of being exercised inside the exponential_backup function

Thanks!

Well don't call it when passing it in, and only call it when you need it:

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')

EDIT : To predefine some arguments in your function you can use partial , that can take in a function apply the arguments to it and return a new function with those arguments already applied, here's an example:

from functools import partial

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

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

g()

This prints

1
2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM