简体   繁体   English

为什么全局变量在Python中的不同函数中不起作用?

[英]Why the global variable is not working in the different functions in Python?

I'm beginner in Python. 我是Python的新手。 I wrote the code like as below just for practice.. Please look on it 我写了如下代码,只是为了练习。

i=1
def wrte():
 global i
 while i<5:
     print "%s .Line..\n" %i
     i+=1

def appnd():
  j=i
  while i in range(i,j+3):
     print "%s .Line..\n" %i
     i+=1

def Main():
   wrte()
   appnd()

Main()

Output is like as below 输出如下

  1 .Line..

    2 .Line..

    3 .Line..

    4 .Line..

**Traceback (most recent call last):
  Line 18, in <module>
    Main()
  Line 16, in Main
    appnd()
  Line 9, in appnd
    j=i
UnboundLocalError: local variable 'i' referenced before assignment**

Expected Result:: The next sequence is should be appended like 预期结果::下一个序列应该像

5. Line..
6. Line..
7. Line..

Please help me on this.. 请帮助我。

You need the global definition in each function in which you use that variable. 在使用该变量的每个函数中都需要global定义。

def appnd():
   global i

Note: If possible, move global variables and related functions into a class. 注意:如果可能,请将全局变量和相关函数移到类中。

adding

global i

before 之前

j=i

should solve the problem 应该解决问题

The scope of your definitions are local. 您的定义范围是本地的。 If you declare a variable as global in a function, it doesn't mean that it would be applied to all the functions. 如果您在函数中将变量声明为全局变量,并不意味着它将应用于所有函数。 You have to declare i as global in your appnd() function too. 您还必须在appnd()函数中将i声明为全局变量。 Having said that, it doesn't mean that your style is right. 话虽如此,这并不意味着您的风格是正确的。 You would rather pass your variables to functions. 您宁愿将变量传递给函数。

The next definition will work: 下一个定义将起作用:

def appnd():
  j=i
  while i in range(i,j+3):
     print "%s .Line..\n" %i

# it would print infinitely, but will work

At compile time Python looks at a way variables are used in the function to define a scope to look for them. 在编译时,Python会查看函数中使用变量的方式,以定义查找它们的范围。 In your definition of appnd it sees assign for i thus trying to threat it as local variable. 在您对appnd的定义中,它会看到为i分配的值,因此试图将其威胁为局部变量。 In my code there are no assignments, so Python simply get the value for i from the parent scope - in this case i is neither local nor global, it called free variable . 在我的代码中没有赋值,因此Python只是从父级作用域获取i的值-在这种情况下, i既不是本地的也不是全局的,它称为free variable Execution model - Scopes and Binding - highly recommended reading. 执行模型-范围和绑定 -强烈建议阅读。

I guess you know when you should use global, otherwise it wouldn't be in your write function. 我想您知道何时应该使用全局,否则它将不在您的写入函数中。 You can omit it if you only read the variable, which I think you want in your append function, but you've got an i+=1 in it, so you do modify it. 如果仅读取变量,可以忽略它,我想在您的append函数中需要该变量,但是其中有i + = 1,因此可以对其进行修改。 Just change append to do: 只需更改追加即可:

for line in range(i, i + 3):
    print "%s .Line..\n" % line

In the appnd function, you must make the global variable i . appnd函数中,必须使全局变量i

i=1
def wrte():
 global i
 while i<5:
     print "%s .Line..\n" %i
     i+=1

def appnd():
    global i
    j=i
    while i in range(i,j+3):
        print "%s .Line..\n" %i
        i+=1

def Main():
   wrte()
   appnd()

Main()

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

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