问题描述
我是 Python 菜鸟,目前正在使用 rabbitMQ。我从rabbitMQ 收到一个payload 并处理它。这是第一个函数
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,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,partner_params = verify_user_id()
是错误的,但我不知道如何将 verify_user_id()
及其参数 ch,body
正确传递到 smile_identity_func()
中。
我希望能够以不必为`ch、method、properties、body'指定值的方式来实现。
解决方法
您要求的是 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,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
这确实为您提供了您想要的功能,能够将某个函数传递给另一个函数,并且其参数已经绑定到该函数。
请注意,从 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,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)