减少 if 语句以最大化 python 中的效率

Reduce if statements to maximize efficiency in python

我已经编写了以下函数来构建参数,我想知道 python 中是否有其他方法可以提高代码效率...

build_parameters(users[keys]["email"], Null , xsmtpapi, Message_Subject, Message_Content, Message_Content, 'support@brightspyre.com', 'BrightSpyre', 'support@brightspyre.com', Null, Null, Null, Null)

这是函数

def build_parameters(to = None, toname = None, x-smtpapi = None, subject = None, text = None, html = None, from = None, cc = None, ccname = None, bcc = None, bccname = None, fromname = None, replyto = None, date = None, files = None, content = None, headers = None):
    param = {}
    if headers:
        param['headers'] = headers
    if content:
        param['content'] = content
    if files:
        param['files'] = files
    if date:
        param['date'] = date
    if replyto:
        param['replyto'] = replyto
    if fromname:
        param['fromname'] = fromname
    if bccname:
        param['bccname'] = bccname
    if bcc:
        param['bcc'] = bcc
    if ccname:
        param['ccname'] = ccname
    if cc:
        param['cc'] = cc
    if from:
        param['from'] = from
    if html:
        param['html'] = html
    if text:
        param['text'] = text
    if subject:
        param['subject'] = subject
    if x-smtpapi:
        param['x-smtpapi'] = x-smtpapi
    if toname:
        param['toname'] = toname
    if to:
        param['to'] = to

    return param

已更新

我已经更新了 @J0HN

描述的代码
_allowed_keys = {'to', 'toname', 'x-smtpapi', 'subject', 'text', 'html', 'from', 'cc', 'ccname', 'bcc', 'bccname', 'fromname', 'replyto', 'date', 'files', 'content', 'headers'}

def build_parameter(**kwargs):
    return {key:value for key, value in kwargs.items() if key in _allowed_keys}


params = build_parameter(to = users[keys]["email"], toname = users[keys]["name"], x-smtpapi = xsmtpapi, subject = Message_Subject,text = Message_Content, html = Message_Content, from = 'support@bs.com', fromname = 'BS', replyto = 'support@bs.com')

错误

params = build_parameter(to = users[keys]["email"],toname = users[keys]["name"], x-smtpapi = xsmtpapi, subject = Message_Subject,text = Message_Content, html = Message_Content, from = '
support@bs.com', fromname = 'BSe', replyto = 'support@bs.com')                                                                                                     
                                                                                                                                                                                        ^    
SyntaxError: invalid syntax 

建个字典就可以了。

params = {"to": "to value",
          "from": "from value}

不需要为此使用方法。

_allowed_keys = {'to', 'toname', 'x-smtpapi', ...]

def build_parameters(**kwargs):
    return {key:value for key, value in kwargs.items() if key in _allowed_keys}

一步一步:

  • 定义 _allowed_keys 以包含所有关键字参数的名称。详见set documentation
  • 将参数替换为**kwargs。请参阅 Understanding kwargs in Python 了解详细信息。
  • 使用 dictionary comprehensionkwargs
  • 中构建新词典
  • items 用于迭代字典的键值对
  • key in _allowed_keys 是不言自明的我相信

因此,此函数接收任意数量的关键字参数,但会过滤掉不在 _allowed_keys.

中的关键字

UPD:好的,from 是关键字,x-smtpapi 不能是关键字参数,因为它包含 -。这是预期的行为,但 canonical way 解决它会使整个方法变得无用。

因此您需要以不同方式表示它们,例如:

_transforms = {'x_smtpapi'='x-smtpapi', `_from`='from'}

def build_parameter(**kwargs):
    return {_transforms.get(key, key):value for key, value in kwargs.items() if _transforms.get(key, key) in _allowed_keys}

然后像这样使用build_parameter(_from='from value', x_smtpapi: 'x-smtpapi value', ...)

但是,我不建议这样做,因为它可能会造成混淆。相反,考虑替代方法,例如构建一个 class 来封装创建 params 字典(并且可能使用它)