简体   繁体   English

用Python传递的参数

[英]Argument passing in Python

I don't quite understand what is going with variable 'current_label', which to me seems to be defined in the enclosing code of function 'ts_r', which would make it visible from inside 'ts_r'? 我不太明白变量'current_label'会发生什么,对我来说似乎是在函数'ts_r'的封闭代码中定义的,这会使它从'ts_r'中可见? But when I run the code below, it complains that local variable 'current_label' is referenced before assignment... Note that it does not complain about 'visited' or 'f', and that it won't complain if I initialize 'current_label' with [len(g)]. 但是,当我运行下面的代码,它抱怨说,局部变量“current_label”被分配之前引用...请注意,它抱怨“访问”或“F”,那如果我初始化“current_label它不会抱怨'与[len(g)]。

def topological_sort(g):

    visited = zeros((len(g)), dtype='int32')
    f = zeros((len(g)), dtype='int32')
    current_label = len(g) # [] so it is seen inside ts_r

    def ts_r(n):
        for nn in [v for v in g[n] if not visited[v]]:
            visited[nn] = 1
            ts_r(nn)
        f[n] = current_label
        current_label -= 1

    for i in range(len(g)):
        if not visited[i]: 
            ts_r(i)

    return f

In case of visited or f you change mutable variables . 如果是visitedf 您可以更改可变变量 In case of current_label you try to re-assign value to global variable without stating it is global. current_label情况下,您尝试将值重新分配给全局变量,而不说明它是全局变量。

Changing variables from outside scopes does not require declaring them global, but reassigning value to a global variable requires declaration, that it is global - otherwise it is treated as local (and in case of referencing before assignement you get such errors). 从外部范围更改变量不需要将它们声明为全局变量,但是将值重新分配给全局变量需要声明,它是全局的 - 否则它被视为本地变量(如果在分配之前引用则会出现此类错误)。

Lets look at the code: 让我们看看代码:

1.    def ts_r(n):
2.        for nn in [v for v in g[n] if not visited[v]]:
3.            visited[nn] = 1
4.            ts_r(nn)
5.        f[n] = current_label
6.        current_label -= 1

In line 5 you assign global variable value to f[n] , but later, in line 6 you try to assign this global variable a value. 在第5行中,您将全局变量值分配给f[n] ,但稍后,在第6行中,您尝试为此全局变量赋值。 You did not tell Python it is global, thus it assumes it is local. 你没有告诉Python它是全局的,因此它假设它是本地的。 But if it is local, you cannnot assign it earlier. 但如果它是本地的,你不能提前分配它。

You have two choices: 你有两个选择:

  1. (probably not the one you are looking for) Use it as local: (可能不是你要找的那个)用它作为本地:

     def ts_r(n): current_label = len(g) # initialize local variable for nn in [v for v in g[n] if not visited[v]]: visited[nn] = 1 ts_r(nn) f[n] = current_label current_label -= 1 
  2. Tell Python it is global variable and you would like to change global variable's value: 告诉Python它是全局变量,你想改变全局变量的值:

     def ts_r(n): global current_label # current_label is now global for nn in [v for v in g[n] if not visited[v]]: visited[nn] = 1 ts_r(nn) f[n] = current_label current_label -= 1 

EDIT : 编辑

After your question was updated, I saw nested functions instead of functions defined in global scope. 在您的问题更新后,我看到嵌套函数而不是全局范围中定义的函数。 Thus the solution with global won't work. 因此,具有global的解决方案将无效。

In Python 3.x you have nonlocal keyword, but you will need to find walkaround in case of Python 2.x. 在Python 3.x中你有nonlocal关键字,但是你需要在Python 2.x的情况下找到walkaround。 Again, you have at least two possibilities: 同样,您至少有两种可能性:

  1. Use mutable variable enclosing immutable you want to change (eg. list with one integer in it). 使用包含您想要更改的不可变的可变变量(例如,列表中包含一个整数)。 Then when you just refer to (and change) the first element of a list. 然后,当您只是引用(并更改)列表的第一个元素时。 Try it. 试试吧。

  2. Another solution is to add an attribute for the wrapping function (function is also a mutable, so you can change it, but you will not pollute global namespace). 另一种解决方案是为包装函数添加一个属性(函数也是可变的,因此您可以更改它,但不会污染全局命名空间)。 Example is here: http://ideone.com/7jGvM . 示例如下: http//ideone.com/7jGvM In your case it may look like this: 在你的情况下,它可能看起来像这样:

     def topological_sort(g): visited = zeros((len(g)), dtype='int32') f = zeros((len(g)), dtype='int32') topological_sort.current_label = len(g) # [] so it is seen inside ts_r def ts_r(n): for nn in [v for v in g[n] if not visited[v]]: visited[nn] = 1 ts_r(nn) f[n] = topological_sort.current_label topological_sort.current_label -= 1 for i in range(len(g)): if not visited[i]: ts_r(i) return f 

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

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