我有这个功能:
@staticmethod
def get_curl_params(url=None, headers=None, post=None, curl_proxy=None, curl_success=None, should_use_cookies=False,
should_follow_location=False, should_include_referer=False, custom_request='Get',
should_verify_ssl=False, head=None, timeout_limit=60, site_rate_limit_seconds=None):
curl_params = CurlParameters()
curl_params.url = url
curl_params.headers = headers
curl_params.post = post
curl_params.curl_proxy = curl_proxy
curl_params.curl_success = curl_success
curl_params.should_use_cookies = should_use_cookies
curl_params.should_follow_location = should_follow_location
curl_params.should_include_referer = should_include_referer
curl_params.custom_request = custom_request
curl_params.should_verify_ssl = should_verify_ssl
curl_params.head = head
curl_params.timeout_limit = timeout_limit
curl_params.site_rate_limit_seconds = site_rate_limit_seconds
return curl_params
它将创建一个新的curl_params对象。现在,我传递的所有参数最终都被分配给curl_params。{{VARIABLE NAME}}。有这样做的捷径吗?用这种方式分配它似乎是重复的和显而易见的。
答案 0 :(得分:1)
我认为这应该起作用; locals
为您提供所有局部变量的dict
(在本例中为函数中的所有变量)。
def get_curl_params(url=None, headers=None, post=None, curl_proxy=None, curl_success=None, should_use_cookies=False,
should_follow_location=False, should_include_referer=False, custom_request='Get',
should_verify_ssl=False, head=None, timeout_limit=60, site_rate_limit_seconds=None):
kwargs = locals()
# if you can pass everything to init, this would be nice
curl_params = CurlParameters(**kwargs)
# otherwise, you can do this:
for name, val in kwargs.items():
setattr(curl_params, name, val)
return curl_params