推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
evanshh
V2EX  ›  Python

Python 函数中的 return 失效了是什么鬼

  •  1
     
  •   evanshh · Oct 26, 2018 · 3928 views
    This topic created in 2760 days ago, the information mentioned may be changed or developed.
    # coding:utf-8
    import numpy as np
    
    
    def drop_water(y, x, array, path):
        s1 = array[y + 1, x - 1] * 5  # 左下
        s2 = array[y + 1, x] * 4  # 下
        s3 = array[y + 1, x + 1] * 3  # 右下
        s4 = array[y, x + 1] * 2  # 右
        s5 = array[y, x - 1] * 1  # 左
        l = [s1, s2, s3, s4, s5]
        w = 4 if sum(l) in [0, 15] else max(l)
        if w == 1:
            next_s = [y, x - 1]
        elif w == 2:
            next_s = [y, x + 1]
        elif w == 3:
            next_s = [y + 1, x + 1]
        elif w == 4:
            next_s = [y + 1, x]
        elif w == 5:
            next_s = [y + 1, x - 1]
        path.append(next_s)
        if next_s[0] == array.shape[0] - 1:
            print(path)
            return path
        else:
            print('a')
            drop_water(next_s[0], next_s[1], array, path)
            print('w')
    
    
    array = np.array([[1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
                       1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1,
                       1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1],
                      [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
                       1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1,
                       1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1],
                      [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0,
                       0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
                       1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1],
                      [1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0,
                       0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
                       1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1]])
    
    s = drop_water(0, 30, array, [])
    print(s)
    

    输出如下:

    a
    a
    [[1, 30], [2, 30], [3, 29]]
    w
    w
    None
    
    ericls
        1
    ericls  
       Oct 26, 2018 via iPhone
    你没有 return 呀
    evanshh
        2
    evanshh  
    OP
       Oct 26, 2018
    @ericls 在 if 里
    ericls
        3
    ericls  
       Oct 26, 2018 via iPhone
    @evanshh 只要进入了 else 就 return 不了了 只剩副作用了
    evanshh
        4
    evanshh  
    OP
       Oct 26, 2018
    @ericls 问题是执行 if 语句后 return 会退出函数,不会执行后续的 else 了呀
    ericls
        5
    ericls  
       Oct 26, 2018 via iPhone
    @evanshh 第一次 call 如果进入了 else 就只剩副作用了
    awanabe
        6
    awanabe  
       Oct 26, 2018
    path 都没打印出来..说明没有走到 if 里面...
    判断逻辑先看看吧...
    evanshh
        7
    evanshh  
    OP
       Oct 26, 2018
    @awanabe [[1, 30], [2, 30], [3, 29]]这显然就是 path..
    bucky
        8
    bucky  
       Oct 26, 2018
    这个问题,王垠都说过了,if else 要成对出现,保证你的逻辑完整,要不然你都发现不了你的 bug 在哪里
    ericls
        9
    ericls  
       Oct 26, 2018 via iPhone
    @evanshh 那是副作用里面的 if
    evanshh
        10
    evanshh  
    OP
       Oct 26, 2018
    @ericls 副作用是啥?
    omph
        11
    omph  
       Oct 26, 2018
    用调试走一遍都清楚了
    evanshh
        12
    evanshh  
    OP
       Oct 26, 2018
    @bucky 我的 if else 成对出现了呀,能否详细解释一下?
    evanshh
        13
    evanshh  
    OP
       Oct 26, 2018
    @omph 走了好几遍仍然懵逼才来问的
    ericls
        14
    ericls  
       Oct 26, 2018 via iPhone   ❤️ 2
    @evanshh 简单说 就是你的 else 里面 没有 return
    Zzdex
        15
    Zzdex  
       Oct 26, 2018
    递归不是你这么玩的
    zsdroid
        16
    zsdroid  
       Oct 26, 2018
    建议百度“递归”先
    ysc3839
        17
    ysc3839  
       Oct 26, 2018 via Android
    从输出能明显看出代码执行到了 else 里面,而你 else 里面没有 return。
    zsdroid
        18
    zsdroid  
       Oct 26, 2018   ❤️ 1
    第 1 次调用 drop_water,进入 else 打印 a
    再次调用 drop_water (第 2 次),进入 else 打印再次 a
    再次调用 drop_water (第 3 次),进入 if 打印 path
    打印第 2 次调用的 w
    打印第 1 次调用的 w
    然后没有 return 所以最后输出 none
    evanshh
        19
    evanshh  
    OP
       Oct 26, 2018 via Android
    @ericls 好了想通了,进了逻辑死胡同了
    awanabe
        20
    awanabe  
       Oct 26, 2018
    递归最后一次走到 else 里面...
    Hombin
        21
    Hombin  
       Oct 27, 2018
    else:
    print('a')
    p = drop_water(next_s[0], next_s[1], array, path)
    print('w')
    return p

    增加一个返回应该就可以了~
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3778 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 116ms · UTC 04:41 · PVG 12:41 · LAX 21:41 · JFK 00:41
    ♥ Do have faith in what you're doing.