简体   繁体   English

递归的“try except”块中的变量减速 function

[英]Variable decleration in a 'try except' block of a recursive function

was trying to declare variable t in the first iteration of a recursion试图在递归的第一次迭代中声明变量 t

class c:
    def __init__(self, a):
        self.n=a
def a():
    t=c(5)
    def b():
        print(t.n)
    b()

does print t打印 t

def d():
    try:
        t
        print(t.n)
    except:
        t=c(5)
        d()

doenst print t不要打印 t

I don't understand the difference and why in the first function does work and the second doesn't我不明白其中的区别以及为什么第一个 function 有效而第二个无效

it won't print t because t is a local variable to each function call and is not recognized in the context of the other calls to d .它不会打印t因为t是每个 function 调用的局部变量,并且在对d的其他调用的上下文中无法识别。 if you want to use it you either have to make it global or pass it as an argument如果你想使用它,你要么将它设为全局,要么将它作为参数传递

def d(t=None):
    try:
        print(t.n)
    except:
        t=c(5)
        d(t=t)
d()

More explanation更多解释

(There is a great video about this by mCoding i recommend you watch, and the content of the channel is pretty good) (mCoding 有一个很棒的视频推荐你看,频道的内容很不错)

Why is it local in d but not b ?为什么它在d而不是b本地?

What happens is: at compile time python will look at your function and see is t defined anywhere in your function?发生的事情是:在编译时 python 将查看您的 function 并查看是否在您的t中的任何地方定义? if yes (the case of d , even if the variable is assigned after it's usage), it will take the value from that scope, if not it will try finding it in the next scope and so on.如果是(对于d的情况,即使变量是在使用后分配的),它将从 scope 中获取值,如果不是,它将尝试在下一个 scope 中找到它,依此类推。 if it doesn't find it, it will assume it's a global.如果找不到它,它将假定它是全局的。

for example:例如:

variable = 1
def method_1():
    print(variable)
    variable = 2
method_1()

will throw an error even tho variable is defined in the global scope because the compiler found that we are assigning variable inside the function即使在全局 scope 中定义了变量,也会抛出错误,因为编译器发现我们在 function 中分配变量

on the other hand:另一方面:

variable = 1
def method_2()
    print(variable)

method_2()

will work because the compiler considers variable global会工作,因为编译器认为变量是全局的

In the second code, when an exception occurs because t is not defined it moves to the except block, and what does the except block do?在第二段代码中,当由于未定义t而发生异常时,它会移动到 except 块,而 except 块会做什么? It creates a variable called t but it is a local variable to that call of the function on the stack and then calls the function again which does the same thing because when you call the function again a new memory is allocated to that function call on the stack and t is not defined in that function call.它创建了一个名为t的变量,但它是堆栈上 function 调用的局部变量,然后再次调用 function 执行相同的操作,因为当您再次调用 function 时,新的 memory 被分配给 function 调用堆栈和t未在该 function 调用中定义。 Hence, it throws an exception and does the same thing again.因此,它抛出一个异常并再次做同样的事情。

在此处输入图像描述

As you can see in the picture an Infinite number of function calls is made until a stack overflow error happens which leads to the termination of your running program because it never breaks out of the try-except block because each time a function call is made t is defined locally to the function call and make another call which doesn't have t defined in it's scope.正如您在图片中看到的那样,会进行无限次 function 调用,直到发生堆栈溢出错误,这会导致正在运行的程序终止,因为它永远不会跳出 try-except 块,因为每次都会调用 function t在本地定义到 function 调用并进行另一个调用,它没有在它的t中定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM