本文介绍了python try/except/else 递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 版本:2.7.操作系统:Windows 10 64 位.

Python version: 2.7. OS: Windows 10 64-bit.

注意:我找到了一种解决下面描述的问题的方法,该方法不使用 try/except/else 语句.我问下面的问题只是因为我很好奇代码的行为方式是什么,以及是否有办法使用 try/except/else 来做我想做的事情.

Note: I have found a way around the issue described below that doesn't use try/except/else statements. I am asking the question below only because I am curious as to why the code behaves the way it does, and if there is a way to do what I am trying to do using try/except/else.

我有一个名为 blah.py 的文件,其代码如下:

I have a file called blah.py, with the following code:

import os

def makeFolder(dirName, num = 0):
    try:
        os.mkdir(dirName + '_' + str(num)) #error if argument of os.mkdir already exists
    except:
        makeFolder(dirName, num = num + 1)
    else:
        return dirName + '_' + str(num)

现在我转到 Powershell,然后输入:

Now I go to Powershell, and type:

import blah
myStr = blah.makeFolder('myFolder')
print myStr
print type(myStr)

它做我所期望的 - 创建了一个名为 myFolder_0 的文件夹,并打印 myFolder_0.现在,仍然在 Powershell 中,我输入:

It does what I would expect - a folder called myFolder_0 is created, and it prints myFolder_0 and <type 'str'>. Now, still in Powershell, I type:

myStr1 = blah.makeFolder('myFolder')
print myStr1
print type(myStr1)

这一次它创建了一个名为 myFolder_1 的文件夹,正如我所期望的,但不是打印 myFolder_1,而是打印 .以后每次我使用 blah.makeFolder('myFolder') 时,它都会继续这样做.

This time it makes a folder called myFolder_1, as I expect, but instead of printing myFolder_1 and <type 'str'>, it prints None and <type 'NoneType'>. It will continue doing this every subsequent time I use blah.makeFolder('myFolder').

如果我将我在 Powershell 中输入的命令放在脚本中,行为也会非常不同.我做了一个名为blah2.py的文件,和blah.py是一样的,只是在最后加了一个脚本:

The behavior is also weirdly different if I put the commands I typed in Powershell inside of the script. I made a file called blah2.py, which is the same as blah.py, but with a script at the end:

import os

def makeFolder(dirName, num = 0):
    try:
        os.mkdir(dirName + '_' + str(num)) #error if argument of os.mkdir already exists
    except:
        makeFolder(dirName, num = num + 1)
    else:
        return dirName + '_' + str(num)

myStr = makeFolder('myFolder')
print myStr
print type(myStr)

myStr1 = makeFolder('myFolder')
print myStr1
print type(myStr1)

然后在 Powershell 中:

Then in Powershell:

python blah2.py

这次它生成 myFolder_0 并打印 myFolder_0,(所以 myStr 块的工作原理是blah.py),然后进入无限递归(所以 myStr1 块不起作用).因此,出于我不明白的原因,行为与交互式会话期间的行为不同.如果我再次输入 python blah2.py,它会生成 myFolder_1 并打印 None (myStr 块),然后再次进入无限递归(myStr1 块).

This time it makes myFolder_0 and prints myFolder_0 and <type 'str'>, (so the myStr block works as in blah.py), and then goes into an infinite recursion (so the myStr1 block doesn't work). So for reasons I don't understand, the behavior is different than it is during the interactive session. If I type python blah2.py again, it makes myFolder_1 and prints None and <type 'NoneType'> (myStr block), then goes into infinite recursion again (myStr1 block).

为什么脚本的行为与交互式会话不同,为什么脚本中会发生无限递归,是否有我的代码版本仍然使用 try/except/else 但有效?

Why does the script behave differently than the interactive session, why does infinite recursion happen in the script, and is there a version of my code that still uses try/except/else, but works?

推荐答案

如果我将 return 添加到递归调用中,您的代码对我来说很好用:

Your code works fine for me if I add a return to the recursive call:

import os

def makeFolder(dirName, num = 0):
    try:
        os.mkdir(dirName + '_' + str(num))
    except OSError:
        return makeFolder(dirName, num = num + 1)
    else:
        return dirName + '_' + str(num)

print(makeFolder('myFolder')) # myFolder_0
print(makeFolder('myFolder')) # myFolder_1

至于为什么你会看到你所看到的......这里肯定有其他事情发生.您为 blah2.py 共享的代码不可能工作,因为 blah 没有在任何地方定义.我的猜测是您在没有意识到的情况下运行了不同的代码.(也许是不同目录中文件的不同副本?)

As to why you're seeing what you're seeing... there's definitely something else going on here. The code you shared for blah2.py couldn't possibly work, since blah isn't defined anywhere. My guess is that you're running different code without realizing it. (Maybe a different copy of the file in a different directory?)

这篇关于python try/except/else 递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 05:17