代码之家  ›  专栏  ›  技术社区  ›  Simeon Leyzerzon mthmulders

如何将语句拆分成多行?

  •  0
  • Simeon Leyzerzon mthmulders  · 技术社区  · 7 年前

    以下表达式的第一行(出现在多行上)应该用什么样的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"
    
    4 回复  |  直到 7 年前
        1
  •  1
  •   D Dhaliwal    7 年前

    可能是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"
    
        2
  •  3
  •   chepner    7 年前

    重构

    我先不打电话 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")
    

    因为每把钥匙 双关语 只是映射到自身,可以替换 那个 用一个简单的 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
    

    最后两个你觉得哪个更可读。这个 双关语 在定义上更为多余,但允许一个赋值 IDP_STRATEGY .

        3
  •  1
  •   W Stokvis    7 年前
    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") 一个变量,所以它被调用一次。

        4
  •  0
  •   Ernie Yang    7 年前

    您可以用“\”显式中断它,也可以通过将条件放在括号内来中断它

    if (a
        ==b
        ==c):