如果/else语句(Python)返回外部

发布于 2025-02-09 23:59:43 字数 564 浏览 0 评论 0原文

当您有以下代码时:

def func(a) -> Optional[S]
    if len(a) == 2:
         x = "bla bla bla"
    else:
        if a.state is True and len(a) == 3:
             s = "hello world"
    return s

注意:S是S的实例。

我有两个问题。请按顺序回答。

  1. 当您在外返回时,如果返回s ,则在下方 else> else 下方,则在何时执行返回?如果是false,将在内部的任何一个条件之后执行返回?

  2. 如果我这样做,它将有什么区别

      def func(a) - >可选[S]
       如果len(a)== 2:
          x =“ bla bla bla”
       别的:
           如果A.STATE为真,并且Len(a)== 3:
              S =“ Hello World”
           返回s
     

When you have the following code:

def func(a) -> Optional[S]
    if len(a) == 2:
         x = "bla bla bla"
    else:
        if a.state is True and len(a) == 3:
             s = "hello world"
    return s

Note: s is an instance of S.

I have two questions. Please answer them in order.

  1. when you have the return s outside the inner if and right below the outer else, then after when would that return be executed? Will the return be executed after either of the conditions in the inner if is false?

  2. What difference will it make if I do this instead, where the return is just outside the inner if now.

    def func(a) -> Optional[S]
       if len(a) == 2:
          x = "bla bla bla"
       else:
           if a.state is True and len(a) == 3:
              s = "hello world"
           return s
    

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

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

发布评论

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

评论(1

你的背包 2025-02-16 23:59:43
  1. 如果/else 代码被执行,将在之后执行,就像在此处放置的任何其他语句一样。请注意,如果条件为true,或者两种条件都是正确的,则您将获得错误,因为在这种情况下,您永远不会分配s
  2. 如果您将返回语句在else中移动:如果条件为true,则将不会执行。在这种情况下,该函数将不会以返回语句结束,因此默认情况下它将返回none。这将避免在条件为真时尝试使用s的错误,但是如果这两个条件都不是,则仍然会出现错误。避免该问题的方法是将返回将最内向移动到:
def func(a) -> Optional[S]
    if len(a) == 2:
        x = "bla bla bla"
    else:
        if a.state is True and len(a) == 3:
            s = "hello world"
            return s

另外,我们通常将 else组合:如果 ,因此代码应为:

def func(a) -> Optional[S]
    if len(a) == 2:
        x = "bla bla bla"
    elif a.state is True and len(a) == 3:
        s = "hello world"
        return s
  1. It will be executed after the if/else code is executed, just like any other statement placed there. Note that you'll get an error if the if condition is true, or if neither condition is true, because in that case you never assign s.
  2. If you move the return statement inside the else: it won't be executed when the if condition is true. IN that case the function will end with no return statement, so it will return None by default. This will avoid the error of trying to use s when the if condition is true, but it will still get an error if neither condition is true. The way to avoid that problem is to move the return into the innermost if:
def func(a) -> Optional[S]
    if len(a) == 2:
        x = "bla bla bla"
    else:
        if a.state is True and len(a) == 3:
            s = "hello world"
            return s

Also, we normally combine else: if into elif, so the code should be:

def func(a) -> Optional[S]
    if len(a) == 2:
        x = "bla bla bla"
    elif a.state is True and len(a) == 3:
        s = "hello world"
        return s
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文