如何将带参数的python函数(闭包)传递给另一个函数?
我是一名Python noob,目前正在与rabbitMQ合作。我从rabbitMQ接收有效负载并对其进行处理。这是第一个函数如何将带参数的python函数(闭包)传递给另一个函数?,python,rabbitmq,pika,Python,Rabbitmq,Pika,我是一名Python noob,目前正在与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
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
现在,我想使用上面的函数向服务器发送请求,如下所示:
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)
我知道body、id\u info、partner\u params=verify\u user\u id是错误的,但我不知道如何正确地将verify\u user\u id及其参数ch、方法、属性、body传递到smile\u identity\u函数中
我希望能够这样做,即我不必为'ch、method、properties、body'指定值。您所要求的是a的经典用例,特别是Python 下面是它的工作原理:
# 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)
结果:
Username x3- | Password f8dJsn9/sd | Email x3-@gmail.com
这确实为您提供了所需的功能,能够将某个函数传递到另一个函数,并且其参数已绑定到该函数
请注意,从所使用的函数中传递的函数的角度来看,闭包和对不带参数的常规函数的引用之间没有区别。然而,这两者在根本上是截然不同的
我想我应该先大致回答这个问题。下面是一个特定于您提供的代码的示例:
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)
您需要的是一个经典的用例,或者说Python 下面是它的工作原理:
# 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)
结果:
Username x3- | Password f8dJsn9/sd | Email x3-@gmail.com
这确实为您提供了所需的功能,能够将某个函数传递到另一个函数,并且其参数已绑定到该函数
请注意,从所使用的函数中传递的函数的角度来看,闭包和对不带参数的常规函数的引用之间没有区别。然而,这两者在根本上是截然不同的
我想我应该先大致回答这个问题。下面是一个特定于您提供的代码的示例:
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)
也许我没有正确理解这个问题,但是为什么你不能将它们传递给smile\u identity\u func,并在该函数中用作verify\u user\u id的参数?也许我没有正确理解这个问题,但是为什么你不能将它们传递给smile\u identity\u func,并在该函数中用作verify\u user\u id的参数?对不起。我的答案的第一个版本展示了如何创建一个可以传递到函数中的闭包,以及如何调用它,但实际上没有演示如何将它传递到另一个函数中。我的答案的新版本显示了>对不起所有人。我的答案的第一个版本展示了如何创建一个可以传递到函数中的闭包,以及如何调用它,但实际上没有演示如何将它传递到另一个函数中。我的答案的新版本表明>