如何将带有参数(闭包)的Python函数传递给另一个函数?

Per*_*ock 2 python rabbitmq pika

我是一名Python菜鸟,目前正在使用rabbitMQ。我从rabbitMQ 收到有效负载并对其进行处理。这是第一个函数

def verify_user_id(ch, method, properties, body):
    print("[*] Received message for NIN verification")
    body = json.loads(body)  # convert JSON string to python object
    parameters = body.get('parameters')
    id_info = {
        'first_name': parameters.get('first_name'),
        'last_name': parameters.get('last_name'),
        'country': parameters.get('country'),
        'id_type': parameters.get('id_type'),
        'id_number': parameters.get('id_number'),
        'entered': parameters.get('entered')
    }
    partner_params = {
        'job_id': parameters.get('job_id'),
        'user_id': parameters.get('user_id'),
        'job_type': parameters.get('job_type')
    }
    return body, id_info, partner_params
Run Code Online (Sandbox Code Playgroud)

现在,我想使用上面的函数向服务器发送请求,如下所示:

def smile_identity_func():
    body, id_info, partner_params = verify_user_id()
    try:
      
         connection = IdApi(str(partner_id), str(api_key), sid_server)
         response = connection.submit_job(partner_params, id_info)
         obj = response.json()
         return jsonify(obj)
        
        ch.basic_ack(delivery_tag=method.delivery_tag)
        print(" [x] Done")
    except ValueError as e:
        # some params are not valid
        return str(e)
    except ServerError as e:
        return str(e)
Run Code Online (Sandbox Code Playgroud)

我知道这body, id_info, partner_params = verify_user_id()是错误的,但我不知道如何正确地将verify_user_id()其参数传递ch, method, properties, bodysmile_identity_func().

我希望能够以一种不必指定“ch、method、properties、body”的值的方式来完成此操作。

Ste*_*eve 5

您要求的是Closure的经典用例,或者是 Python 特有的A Python Closure

其工作原理如下:

# Create a closure on the `request` function that hard-codes the passed in
# parameters as the context variables to be used when executing the function
def get_request_function(username, password, email):

    # The standard function to call
    def request():
        print("Username {} | Password {} | Email {}".format(username, password, email))

    # return the closure on 'request'
    return request

# Another function that simply calls the function represented by the
# reference passed to it.
def used_passed_in_function(f):
    f()

# Get an instance of our closure, a call to "request", that bakes in
# the context variables we provide.
f = get_request_function("x3-", "f8dJsn9/sd", "x3-@gmail.com")

# Finally, call our sample function that calls whatever function is passed
# to it, passing in our closure to be the function that is called.
used_passed_in_function(f)
Run Code Online (Sandbox Code Playgroud)

结果:

Username x3- | Password f8dJsn9/sd | Email x3-@gmail.com
Run Code Online (Sandbox Code Playgroud)

这确实为您提供了您想要的功能,即能够将某个函数传递到另一个函数,并且其参数已经绑定到该函数。

请注意,从 的角度来看used_passed_in_function,闭包和对不带参数的常规函数​​的引用没有区别。然而,两者本质上是非常不同的。

我想我应该先笼统地回答这个问题。以下是特定于您提供的代码的示例:

def smile_identity_func(verify_func):
    # This call to "verify_func" is a call to "verify_user_id" with a set of
    # parameters hard-coded into the call.
    body, id_info, partner_params = verify_func()
    ...

def create_verify_user_id_closure(ch, method, properties, body):
    def baked_verify_user_id():
        return verify_user_id(ch, method, properties, body)
    return baked_verify_user_id

verify_id = create_verify_user_id_closure("sample-ch", "sample-method", "sample-properties", "sample-body")
smile_identity_func(verify_id)
Run Code Online (Sandbox Code Playgroud)