如何优雅地解决pylint(Too-Many-Return Statements)?

发布于 2025-01-24 10:51:44 字数 437 浏览 2 评论 0原文

太多的返回陈述的例子如上所述,但我的情况如下:如何使功能更美丽?

def func():
    # do something1
    ret1 = do_something1()
    if ret1 != 0:
        return ret1

    # do something2
    ret2 = do_something2()
    if ret2 != 0:
        return ret2
    # ......
    return 0

enter image description here
The example of too-many-return-statements is as above, but my scenario is as follows, how to make the function more beautiful?

def func():
    # do something1
    ret1 = do_something1()
    if ret1 != 0:
        return ret1

    # do something2
    ret2 = do_something2()
    if ret2 != 0:
        return ret2
    # ......
    return 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

再见回来 2025-01-31 10:51:44

您可以尝试这样的事情:

def foo(x):
    nums = ['one','two','three','four','five','six','seven']
    return 'This is ' + nums[x-1]

解决您的示例。您可以这样解决您的方案:

def func():
    functions = [do_something1,do_something2,...]
    for function in functions:
        ret = function()
        if ret != 0:
            return ret
    return 0

You could try something like this:

def foo(x):
    nums = ['one','two','three','four','five','six','seven']
    return 'This is ' + nums[x-1]

to solve your example. And you could solve your scenario like this:

def func():
    functions = [do_something1,do_something2,...]
    for function in functions:
        ret = function()
        if ret != 0:
            return ret
    return 0
月亮是我掰弯的 2025-01-31 10:51:44

查看此示例

它转换为:

def func(x):
    if x == 1:
        return "hi1"
    if x == 2:
        return "hi2"
    if x == 3:
        return "hi3"
    if x == 4:
        return "hi4"
    if x == 5:
        return "hi5"
    if x == 6:
        return "hi6"
    if x == 7:
        return "hi7"

to:

d = {1: "hi1", 2: "hi2", 3: "hi3", 4: "hi4", 5: "hi5", 6: "hi6", 7: "hi7"}
def func(x):
    return d[x]

只是为了使其沉默,另一个选择是:

def func(x):
    if x == something1:
        res = "something1"
    elif x == something2:
        res = "something2"
    elif x == something3:
        res = "something3"
    elif x == something4:
        res = "something4"
    elif x == something5:
        res = "something5"
    elif x == something6:
        res = "something6"
    elif x == something7:
        res = "something7"
    else:
        res = "default"
    return res

您也可以在settings.json文件中静音。

    "python.linting.pylintArgs": [
        "--disable=R0911"
    ],

Check out this example.

It converted:

def func(x):
    if x == 1:
        return "hi1"
    if x == 2:
        return "hi2"
    if x == 3:
        return "hi3"
    if x == 4:
        return "hi4"
    if x == 5:
        return "hi5"
    if x == 6:
        return "hi6"
    if x == 7:
        return "hi7"

to:

d = {1: "hi1", 2: "hi2", 3: "hi3", 4: "hi4", 5: "hi5", 6: "hi6", 7: "hi7"}
def func(x):
    return d[x]

Just to silence it another option is:

def func(x):
    if x == something1:
        res = "something1"
    elif x == something2:
        res = "something2"
    elif x == something3:
        res = "something3"
    elif x == something4:
        res = "something4"
    elif x == something5:
        res = "something5"
    elif x == something6:
        res = "something6"
    elif x == something7:
        res = "something7"
    else:
        res = "default"
    return res

You can also silence it in settings.json file if you think it's too strict rule which I think it is:

    "python.linting.pylintArgs": [
        "--disable=R0911"
    ],
蒗幽 2025-01-31 10:51:44
def func():
  res = default_res

  if foo:
    res = res_foo
    
  elif bar:
    res = res_bar
    
  return res

我建议这样做

def func():
  res = default_res

  if foo:
    res = res_foo
    
  elif bar:
    res = res_bar
    
  return res

I recommend doing it this way

遥远的绿洲 2025-01-31 10:51:44

使用某种形式的查找是处理此问题的方法。词典是一个很好的通用想法。当查找“键”是整数时,列表很有用。

但是,

当使用查找技术时,您必须考虑您的“键”可能不可用。如果您的字典(例如)具有7个整数键(1-> 7包含),并且您的函数的值为8,那么您将要做什么?您可以允许异常并分别处理该例外,或者您需要避免异常并返回一些默认值,也许是隐式的。

Using some form of lookup is the way to handle this. A dictionary is a good general purpose idea. A list is useful when the lookup 'key' is an integer.

HOWEVER

When using a lookup technique you have to consider that your 'key' may not be available. If your dictionary (for example) has 7 integer keys (1->7 inclusive) and your function is passed a value of 8, what are you going to do? You could allow the exception and deal with that separately or you need to avoid an exception and return some default value, perhaps implicitly.

平生欢 2025-01-31 10:51:44

通常,您可以通过Pylint文档使用拟议的解决方案:
https:> https:https:https:///pylint.pycqa。 org/en/最新/user_guide/messages/rebactor/too-ny-return-statements.html

但是,在某种程度上,恕我直言,这是不可能使用的。

我不为您的特定情况进行夸大,但是您可以使用这样的评论在特定的代码行中禁用孔。

def func():  #pylint: disable=R0911
    # do something1
    ret1 = do_something1()
    if ret1 != 0:
        return ret1

    # do something2
    ret2 = do_something2()
    if ret2 != 0:
        return ret2
    # ......
    return 0

In general you can use the proposed solution by pylint documentation:
https://pylint.pycqa.org/en/latest/user_guide/messages/refactor/too-many-return-statements.html

But IMHO in some case it's impossible to use.

I don't kwonw your specific case, but you can disable pylint in a specific line of code using a comment like this.

def func():  #pylint: disable=R0911
    # do something1
    ret1 = do_something1()
    if ret1 != 0:
        return ret1

    # do something2
    ret2 = do_something2()
    if ret2 != 0:
        return ret2
    # ......
    return 0
月依秋水 2025-01-31 10:51:44

由于python 3.10,您可以使用匹配其他编程语言的能力。
您的代码可以转换为这样的东西:

def foo(x):
    match x:
        case 1:
            ret = "This is one."
        case 2:
            ret = "This is two."
        case 3:
            ret = "This is three."
        case 4:
            ret = "This is four."
        case 5:
            ret = "This is five."
        case 6:
            ret = "This is six."
        case 7:
            ret = "This is seven."
        case _:
            ret = "Unknown."
    return ret

Since python 3.10 you can use match which is very similar to switch/case capability in other programming languages.
Your code can be converted to something like this:

def foo(x):
    match x:
        case 1:
            ret = "This is one."
        case 2:
            ret = "This is two."
        case 3:
            ret = "This is three."
        case 4:
            ret = "This is four."
        case 5:
            ret = "This is five."
        case 6:
            ret = "This is six."
        case 7:
            ret = "This is seven."
        case _:
            ret = "Unknown."
    return ret
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文