如何将语句拆分为多行?

2024-06-16 09:48:38 发布

您现在位置:Python中文网/ 问答频道 /正文

正确的Pythonic方法是什么来打断下面表达式的第一行(出现在多行上),使其更具可读性:

if props.getProperty("app.auth.idp.strategy") == '' or props.getProperty("app.auth.idp.strategy") == 'saml/simpleSAMLphp' and PROXYING_MECHANISM == "ngrok":
    IDP_STRATEGY = "saml/simpleSAMLphp"
elif props.getProperty("app.auth.idp.strategy") == 'saml/gsuite':
    IDP_STRATEGY = "saml/gsuite"
elif props.getProperty("app.auth.idp.strategy") == 'saml/remote-simpleSAMLphp':
    IDP_STRATEGY = "saml/remote-simpleSAMLphp"
else:
     IDP_STRATEGY = "saml"

Tags: 方法authappremotesamlidppropspythonic
3条回答
prop_var = props.getProperty("app.auth.idp.strategy")    
if prop_var == '' or prop_var == 'saml/simpleSAMLphp' and PROXYING_MECHANISM == "ngrok":
        IDP_STRATEGY = "saml/simpleSAMLphp"
    elif prop_var == 'saml/gsuite':
        IDP_STRATEGY = "saml/gsuite"
    elif prop_var == 'saml/remote-simpleSAMLphp':
        IDP_STRATEGY = "saml/remote-simpleSAMLphp"
    else:
         IDP_STRATEGY = "saml"

如注释中所述,在每行末尾添加一个\。您还可以用一个变量替换每个getProperty("app.auth.idp.strategy"),这样就只调用一次。你知道吗

根据PEP8可能是这样的

if props.getProperty("app.auth.idp.strategy") == '' \
        or props.getProperty("app.auth.idp.strategy") == 'saml/simpleSAMLphp' \
        and PROXYING_MECHANISM == "ngrok":
    IDP_STRATEGY = "saml/simpleSAMLphp"
elif props.getProperty("app.auth.idp.strategy") == 'saml/gsuite':
    IDP_STRATEGY = "saml/gsuite"
elif props.getProperty("app.auth.idp.strategy") == 'saml/remote-simpleSAMLphp':
    IDP_STRATEGY = "saml/remote-simpleSAMLphp"
else:
     IDP_STRATEGY = "saml"

重构

首先,我不会重复调用props.getProperty("app.auth.idp.strategy")。打一次电话,你马上就没有理由分线了。你知道吗

strategy = props.getProperty("app.auth.idp.strategy")
if not strategy or strategy == 'saml/simpleSAMLphp' and PROXYING_MECHANISM == "ngrok":
    IDP_STRATEGY = "saml/simpleSAMLphp"
elif strategy == 'saml/gsuite':
    IDP_STRATEGY = "saml/gsuite"
elif strategy == 'saml/remote-simpleSAMLphp':
    IDP_STRATEGY = "saml/remote-simpleSAMLphp"
else:
     IDP_STRATEGY = "saml"

行延拓

对于第一个长行,您的选项是行继续,可以是显式的:

if not strategy or \
   strategy == 'saml/simpleSAMLphp' and \
   PROXYING_MECHANISM == "ngrok":

或隐式括号内:

if (not strategy or
    strategy == 'saml/simpleSAMLphp' and
    PROXYING_MECHANISM == "ngrok"):

使用查找,而不是重复比较

一个更好的选择是用dict查找替换一个长字符串:

strategies = {
    "saml/gsuite": "saml/gsuite",
    "saml/remote-simpleSAMLphp": "saml/remote-simpleSAMLphp",
}
if PROXYING_MECHANISM == "ngrok":
    strategies['saml/simpleSAMLphp'] = 'saml/simpleSAMLphp'

IDP_STRATEGY = strategies.get(props.getProperty("app.auth.idp.strategy"), "saml")

因为dict的每个键都映射到它自己,所以可以用一个简单的set查找来替换。你知道吗

strategies = {
    "saml/gsuite",
    "saml/remote-simpleSAMLphp",
}
if PROXYING_MECHANISM == "ngrok":
    strategies.add('saml/simpleSAMLphp')

IDP_STRATEGY = "saml"

strategy = props.getProperty("app.auth.idp.strategy")
if strategy in strategies:
    IDP_STRATEGY = strategy

从最后两个选项中选出你认为更具可读性的。dict在其定义中更为冗余,但允许对IDP_STRATEGY进行单个赋值。你知道吗

相关问题 更多 >